/**
 * PHP's number_format function rewritten for Javascript
 * 
 * @param {Number} a The number to format
 * @param {Number} b The number of decimal points
 * @param {String} c The separator for the decimal point
 * @param {String} d The thousands separator
 * 
 * @see http://mathiasbynens.be/archive/2006/01/js-number-format
 */
var number_format = function(a, b, c, d) {
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if (!f[0]) {
		f[0] = '0';
	}
	if (!f[1]) {
		f[1] = '';
	}
	if (f[1].length < b) {
		g = f[1];
		for (i=f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j+=3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 === 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '' : c;
	return f[0] + c + f[1];
}

document.observe('dom:loaded', function() {
	
	if($('contact-lightwindow-voordeelcalculator')) {
		$('contact-lightwindow-voordeelcalculator').observe('click', function(e) {
			Event.stop(e);
			myLightWindow.activateWindow({
				href: this.href,
				title: this.title,
				width: 600,
				height: 570,
				type: 'external'
			});
		});
	}
	
	$('usage-time-variable').observe('change', function(e) {
		parseForm(e);
	});
	$('km-year-variable').observe('change', function(e) {
		parseForm(e);
	});
	$('km-liter-variable').observe('change', function(e) {
		parseForm(e);
	});
	$('fuel-type-variable').observe('change', function(e) {
		parseForm(e);
	});
	$('fuel-cost-variable').observe('change', function(e) {
		parseForm(e);
	});
	$('road-tax-variable').observe('change', function(e) {
		parseForm(e);
	});
	$('insurance-type-variable').observe('change', function(e) {
		parseForm(e);
	});
	$('insurance-cost-variable').observe('change', function(e) {
		parseForm(e);
	});
	/*$('bluemotion-list').observe('change', function(e) {					
		parseForm(e);
	});*/

	$('submit-list-item').hide();
});

function parseForm(e) {
	if(e.target.id == 'usage-time-variable') {
		$('usage-time-fixed').innerHTML = e.target.value + ' jaar';
		$('usage-time-fixed-ecomotive').innerHTML = e.target.value + ' jaar';
		$('usage-time-fixed-greenline').innerHTML = e.target.value + ' jaar';
		$('cost-usage-time-variable-label').innerHTML = 'Autokosten over ' + e.target.value + ' jaar:';
		$('gain-usage-time-label').innerHTML = 'Autokosten over ' + e.target.value + ' jaar:';
	}
	if(e.target.id == 'km-year-variable') {
		$('km-year-fixed').innerHTML = number_format(e.target.value, 0, ',', '.') + ' km';
		$('km-year-fixed-ecomotive').innerHTML = number_format(e.target.value, 0, ',', '.') + ' km';
		$('km-year-fixed-greenline').innerHTML = number_format(e.target.value, 0, ',', '.') + ' km';
	}
	if(e.target.id == 'fuel-type-variable') {
		if(e.target.value == 'gasoline') {
			$('fuel-cost-variable').innerHTML = '&euro; 1,53';
		} else if(e.target.value == 'diesel') {
			$('fuel-cost-variable').innerHTML = '&euro; 1,19';
		}
	}
	if(e.target.id == 'insurance-type-variable') {
		if(e.target.value == 'allrisk') {
			$('insurance-type-fixed').innerHTML = 'Allrisk';
			$('insurance-type-fixed-ecomotive').innerHTML = 'Allrisk';
			$('insurance-type-fixed-greenline').innerHTML = 'Allrisk';
			$('insurance-cost-fixed').innerHTML = '27,35';
			$('insurance-cost-fixed-ecomotive').innerHTML = '26,27';
			$('insurance-cost-fixed-greenline').innerHTML = '32,21';
		} else if(e.target.value == 'waplus') {
			$('insurance-type-fixed').innerHTML = 'WA+';
			$('insurance-type-fixed-ecomotive').innerHTML = 'WA+';
			$('insurance-type-fixed-greenline').innerHTML = 'WA+';
			$('insurance-cost-fixed').innerHTML = '18,50';
			$('insurance-cost-fixed-ecomotive').innerHTML = '19,11';
			$('insurance-cost-fixed-greenline').innerHTML = '23,05';
		}
	}

	var postBody = $('bluemotion_form').serialize();

	new Ajax.Request('http://www.huiskes-kokkeler.nl/voordeelcalculator/json', { 
		'method': 'post',
		'postBody': postBody,
		'onSuccess': function(response) {
			console.log(response.responseText);
			var totals = response.responseText.evalJSON(true);
			$('cost-year-variable').innerHTML = totals['cost-year-variable'];
			$('cost-year-fixed').innerHTML = totals['cost-year-fixed'];
			$('cost-year-fixed-ecomotive').innerHTML = totals['cost-year-fixed-ecomotive'];
			$('cost-year-fixed-greenline').innerHTML = totals['cost-year-fixed-greenline'];
			$('cost-usage-time-variable').innerHTML = totals['cost-usage-time-variable'];
			$('cost-usage-time-fixed').innerHTML = totals['cost-usage-time-fixed'];
			$('cost-usage-time-fixed-ecomotive').innerHTML = totals['cost-usage-time-fixed-ecomotive'];
			$('cost-usage-time-fixed-greenline').innerHTML = totals['cost-usage-time-fixed-greenline'];
			$('gain-year').innerHTML = totals['gain-year'];
			$('gain-year-ecomotive').innerHTML = totals['gain-year-ecomotive'];
			$('gain-year-greenline').innerHTML = totals['gain-year-greenline'];
			$('gain-usage-time').innerHTML = totals['gain-usage-time'];
			$('gain-usage-time-ecomotive').innerHTML = totals['gain-usage-time-ecomotive'];
			$('gain-usage-time-greenline').innerHTML = totals['gain-usage-time-greenline'];
		}
	});
}
