function isInt (str) {

	var i = parseInt (str);

	if (isNaN (i)) return false;

	i = i . toString ();
	if (i != str) return false;

	return true;

}

function num_format (x) {

        var str = new String (x);
        var newstr = "";
        var nr_caractere = str.length;

        if (nr_caractere <= 3)
                return str;

        for (var i = 0; i < nr_caractere; i++) {
                if (! ((nr_caractere - i) % 3) && i > 0)
                        newstr = newstr + ".";
                newstr = newstr + str.charAt(i);
        }

        return (newstr);

}

function forceNum (number) {

	return number.length ? number : 0;

}
	var cillinders = {
			 2 : {
					 2: {
				'min'	: '0',
				'max'	: '1600',
				'price'	: '1.2'
			}
					,  3: {
				'min'	: '1601',
				'max'	: '2000',
				'price'	: '1.2'
			}
					,  4: {
				'min'	: '2000',
				'max'	: '0',
				'price'	: '1.2'
			}
				}
			,  3 : {
					 5: {
				'min'	: '0',
				'max'	: '1600',
				'price'	: '0.5'
			}
					,  6: {
				'min'	: '1601',
				'max'	: '2000',
				'price'	: '0.7'
			}
					,  7: {
				'min'	: '2001',
				'max'	: '2500',
				'price'	: '1'
			}
					,  8: {
				'min'	: '2501',
				'max'	: '3000',
				'price'	: '1.1'
			}
					,  9: {
				'min'	: '3000',
				'max'	: '0',
				'price'	: '1.2'
			}
				}
			,  4 : {
					 10: {
				'min'	: '0',
				'max'	: '1600',
				'price'	: '0.5'
			}
					,  11: {
				'min'	: '1601',
				'max'	: '2000',
				'price'	: '0.7'
			}
					,  12: {
				'min'	: '2001',
				'max'	: '2500',
				'price'	: '1'
			}
					,  13: {
				'min'	: '2501',
				'max'	: '3000',
				'price'	: '1.1'
			}
					,  14: {
				'min'	: '3000',
				'max'	: '0',
				'price'	: '1.2'
			}
				}
			,  5 : {
					 15: {
				'min'	: '0',
				'max'	: '1600',
				'price'	: '0.35'
			}
					,  16: {
				'min'	: '1601',
				'max'	: '2000',
				'price'	: '0.5'
			}
					,  17: {
				'min'	: '2001',
				'max'	: '2500',
				'price'	: '0.7'
			}
					,  18: {
				'min'	: '2501',
				'max'	: '3000',
				'price'	: '1'
			}
					,  19: {
				'min'	: '3000',
				'max'	: '0',
				'price'	: '1.1'
			}
				}
			,  6 : {
					 20: {
				'min'	: '0',
				'max'	: '1600',
				'price'	: '0.15'
			}
					,  21: {
				'min'	: '1601',
				'max'	: '2000',
				'price'	: '0.35'
			}
					,  22: {
				'min'	: '2001',
				'max'	: '2500',
				'price'	: '0.5'
			}
					,  23: {
				'min'	: '2501',
				'max'	: '3000',
				'price'	: '0.7'
			}
					,  24: {
				'min'	: '3000',
				'max'	: '0',
				'price'	: '1'
			}
				}
		}

	var ages = {
			 '1' : {
			'min'		: '0',
			'max'		: '3',
			'coef'		: '0.5',
			'reduction'	: '10'
		}
			,  '2' : {
			'min'		: '3',
			'max'		: '6',
			'coef'		: '1.5',
			'reduction'	: '20'
		}
			,  '3' : {
			'min'		: '6',
			'max'		: '12',
			'coef'		: '2',
			'reduction'	: '30'
		}
			,  '4' : {
			'min'		: '12',
			'max'		: '24',
			'coef'		: '2.1',
			'reduction'	: '35'
		}
			,  '5' : {
			'min'		: '24',
			'max'		: '36',
			'coef'		: '2.2',
			'reduction'	: '40'
		}
			,  '6' : {
			'min'		: '36',
			'max'		: '48',
			'coef'		: '2.3',
			'reduction'	: '42'
		}
			,  '7' : {
			'min'		: '48',
			'max'		: '60',
			'coef'		: '2.4',
			'reduction'	: '44'
		}
			,  '8' : {
			'min'		: '60',
			'max'		: '72',
			'coef'		: '2.5',
			'reduction'	: '46'
		}
			,  '9' : {
			'min'		: '72',
			'max'		: '84',
			'coef'		: '2.5',
			'reduction'	: '48'
		}
			,  '14' : {
			'min'		: '84',
			'max'		: '96',
			'coef'		: '2.5',
			'reduction'	: '50'
		}
			,  '15' : {
			'min'		: '96',
			'max'		: '108',
			'coef'		: '2.5',
			'reduction'	: '51'
		}
			,  '12' : {
			'min'		: '108',
			'max'		: '120',
			'coef'		: '2.5',
			'reduction'	: '52'
		}
			,  '13' : {
			'min'		: '120',
			'max'		: '0',
			'coef'		: '2.5',
			'reduction'	: '53'
		}
		}

	function updateFields() {

		form 		= document.forms['specialTaxesForm'];

		polId		= form.pollutionId;

		cmcube		= form.cmc;
		carAge		= form.age;

		cmcubePrice	= form.cmcPrice;
		carAgeCoef	= form.coef;
		carAgeReduction	= form.reduction;

		tax		= document.getElementById('specialTax');

		if (
			cmcube.value.length &&
			!isInt (cmcube.value)
		) {

			cmcube.value = cmcube.value.substring (0, cmcube.value.length - 1);
			return;

		}

		if (!polId.value.length) return;
		polCillinders = cillinders[polId.value];

		theValue = forceNum (cmcube.value);
		for (i in polCillinders) {

			if (
				(parseInt (theValue) >= parseInt (polCillinders[i]['min']) &&
				parseInt (theValue) <= parseInt (polCillinders[i]['max'])) ||
				(!parseInt (polCillinders[i]['max']) &&
				parseInt (theValue) > parseInt (polCillinders[i]['min']))
			) {

				price = polCillinders[i]['price'];
				break;

			}

		}

		try {
			cmcubePrice.value = price;
		} catch(e) {
			cmcubePrice.value = '';
		}

		if (!carAge.value.length)
			for (i in ages) {

				carAge.value = i;
				break;

			}

		carAgeCoef.value	= ages[carAge.value]['coef'];
		carAgeReduction.value	= ages[carAge.value]['reduction'];

		tax.innerHTML =
			parseFloat (forceNum (cmcube.value)) *
			parseFloat (forceNum (cmcubePrice.value)) *
			parseFloat (forceNum (carAgeCoef.value)) *
			(100 - parseFloat (forceNum (carAgeReduction.value))) /
			100;

	}




	var newCillinders = {
			 6 : {
					 20: {
				'min'	: '0',
				'max'	: '1600',
				'price'	: '0.15'
			}
					,  21: {
				'min'	: '1601',
				'max'	: '2000',
				'price'	: '0.35'
			}
					,  22: {
				'min'	: '2001',
				'max'	: '2500',
				'price'	: '0.5'
			}
					,  23: {
				'min'	: '2501',
				'max'	: '3000',
				'price'	: '0.7'
			}
					,  24: {
				'min'	: '3000',
				'max'	: '0',
				'price'	: '1'
			}
				}
			,  5 : {
					 15: {
				'min'	: '0',
				'max'	: '1600',
				'price'	: '0.5'
			}
					,  16: {
				'min'	: '1601',
				'max'	: '2000',
				'price'	: '0.6'
			}
					,  17: {
				'min'	: '2001',
				'max'	: '2500',
				'price'	: '0.9'
			}
					,  18: {
				'min'	: '2501',
				'max'	: '3000',
				'price'	: '1.1'
			}
					,  19: {
				'min'	: '3000',
				'max'	: '0',
				'price'	: '1.3'
			}
				}
			,  4 : {
					 10: {
				'min'	: '0',
				'max'	: '1600',
				'price'	: '1.3'
			}
					,  11: {
				'min'	: '1601',
				'max'	: '2000',
				'price'	: '1.4'
			}
					,  12: {
				'min'	: '2001',
				'max'	: '2500',
				'price'	: '1.5'
			}
					,  13: {
				'min'	: '2501',
				'max'	: '3000',
				'price'	: '1.6'
			}
					,  14: {
				'min'	: '3000',
				'max'	: '0',
				'price'	: '1.7'
			}
				}
			,  3 : {
					 5: {
				'min'	: '0',
				'max'	: '1600',
				'price'	: '1.8'
			}
					,  6: {
				'min'	: '1601',
				'max'	: '2000',
				'price'	: '1.8'
			}
					,  7: {
				'min'	: '2001',
				'max'	: '2500',
				'price'	: '1.8'
			}
					,  8: {
				'min'	: '2501',
				'max'	: '3000',
				'price'	: '1.8'
			}
					,  9: {
				'min'	: '3000',
				'max'	: '0',
				'price'	: '1.8'
			}
				}
			,  2 : {
					 2: {
				'min'	: '0',
				'max'	: '1600',
				'price'	: '2'
			}
					,  3: {
				'min'	: '1601',
				'max'	: '2000',
				'price'	: '2'
			}
					,  4: {
				'min'	: '2000',
				'max'	: '0',
				'price'	: '2'
			}
				}
		}

	var newAges = {
			 '1' : {
			'min'		: '0',
			'max'		: '3',
			'coef'		: '0.9',
			'reduction'	: '15'
		}
			,  '2' : {
			'min'		: '3',
			'max'		: '6',
			'coef'		: '0.9',
			'reduction'	: '15'
		}
			,  '9' : {
			'min'		: '6',
			'max'		: '12',
			'coef'		: '1.8',
			'reduction'	: '25'
		}
			,  '3' : {
			'min'		: '12',
			'max'		: '24',
			'coef'		: '1.8',
			'reduction'	: '25'
		}
			,  '10' : {
			'min'		: '24',
			'max'		: '36',
			'coef'		: '2.3',
			'reduction'	: '32'
		}
			,  '5' : {
			'min'		: '36',
			'max'		: '48',
			'coef'		: '2.3',
			'reduction'	: '32'
		}
			,  '11' : {
			'min'		: '48',
			'max'		: '60',
			'coef'		: '2.5',
			'reduction'	: '43'
		}
			,  '7' : {
			'min'		: '60',
			'max'		: '72',
			'coef'		: '2.5',
			'reduction'	: '43'
		}
			,  '8' : {
			'min'		: '72',
			'max'		: '84',
			'coef'		: '2.7',
			'reduction'	: '47'
		}
			,  '12' : {
			'min'		: '84',
			'max'		: '96',
			'coef'		: '2.7',
			'reduction'	: '47'
		}
			,  '13' : {
			'min'		: '96',
			'max'		: '108',
			'coef'		: '2.7',
			'reduction'	: '47'
		}
			,  '14' : {
			'min'		: '108',
			'max'		: '120',
			'coef'		: '2.7',
			'reduction'	: '47'
		}
			,  '15' : {
			'min'		: '120',
			'max'		: '0',
			'coef'		: '2.7',
			'reduction'	: '47'
		}
		}

	function updateNewFields() {

		form 			= document.forms['newSpecialTaxesForm'];

		newPolId		= form.newPollutionId;

		newCmcube		= form.newCmc;
		newCarAge		= form.newAge;

		newCmcubePrice		= form.newCmcPrice;
		newCarAgeCoef		= form.newCoef;
		newCarAgeReduction	= form.newReduction;

		newTax		= document.getElementById('newSpecialTax');

		if (
			newCmcube.value.length &&
			!isInt (newCmcube.value)
		) {

			newCmcube.value = newCmcube.value.substring (0, newCmcube.value.length - 1);
			return;

		}

		if (!newPolId.selectedIndex) return;
		newPolCillinders = newCillinders[newPolId.value];

		theValue = forceNum (newCmcube.value);
		for (i in newPolCillinders) {

			if (
				(parseInt (theValue) >= parseInt (newPolCillinders[i]['min']) &&
				parseInt (theValue) <= parseInt (newPolCillinders[i]['max'])) ||
				(!parseInt (newPolCillinders[i]['max']) &&
				parseInt (theValue) > parseInt (newPolCillinders[i]['min']))
			) {

				price = newPolCillinders[i]['price'];
				break;

			}

		}

		try {
			newCmcubePrice.value = price;
		} catch(e) {
			newCmcubePrice.value = '';
		}

		if (!newCarAge.selectedIndex) newCarAge.selectedIndex = 1;

		newCarAgeCoef.value		= newAges[newCarAge.value]['coef'];
		newCarAgeReduction.value	= newAges[newCarAge.value]['reduction'];

		newTax.innerHTML =
			parseFloat (forceNum (newCmcube.value)) *
			parseFloat (forceNum (newCmcubePrice.value)) *
			parseFloat (forceNum (newCarAgeCoef.value)) *
			(100 - parseFloat (forceNum (newCarAgeReduction.value))) /
			100;

		form 			= document.forms['specialTaxesForm'];
		form.pollutionId.selectedIndex	= newPolId.selectedIndex;
		form.cmc.value			= newCmcube.value;
		form.age.selectedIndex		= newCarAge.selectedIndex;

		updateFields ();

	}