﻿jQuery.noConflict();
var type='beides';
jQuery(document).ready( function() {
	/*
	var faktor = [
		'18': [
			'1000': '7.21',
			'2500': '6.33',
			'5000': '6.06',
			'10000': '5.98',
			'15000': '5.92'
		],
		'24': [
			'1000': '5.80',
			'2500': '4.96',
			'5000': '4.68',
			'10000': '4.60',
			'15000': '4.53'
		]
	];
	*/
	if( jQuery( '#type' ).val() == 'leasing' ) {
		type='leasing';
		calc_leasing();
		jQuery('#r_laufzeit').bind( 'change', function() {calc_leasing();});
		jQuery('#r_preis').bind( 'change', function() {calc_leasing();});
	} else if( jQuery( '#type' ).val() == 'vorteil' ) {
		type='vorteil';
		calc_vorteil();
		jQuery('#r_v_preis').bind( 'change', function() {calc_vorteil();});
		jQuery('#r_leistung').bind( 'change', function() {calc_vorteil();});
		jQuery('#r_verbrauch').bind( 'change', function() {calc_vorteil();});
		jQuery('#r_ersparnis').bind( 'change', function() {calc_vorteil();});
	} else if( jQuery( '#type' ).val() == 'beides' ) {
		calc_leasing();
		calc_vorteil();
		jQuery('#r_laufzeit').bind( 'change', function() {calc_leasing();});
		jQuery('#r_preis').bind( 'change', function() {calc_leasing();});
		jQuery('#r_v_preis').bind( 'change', function() {calc_vorteil();});
		jQuery('#r_leistung').bind( 'change', function() {calc_vorteil();});
		jQuery('#r_verbrauch').bind( 'change', function() {calc_vorteil();});
		jQuery('#r_ersparnis').bind( 'change', function() {calc_vorteil();});
	}
});

function calc_leasing() {
	var laufzeit = jQuery('#r_laufzeit').val();
	var num_b = new NumberFormat();
	num_b.setInputDecimal(',');
	num_b.setNumber( jQuery('#r_preis').val() );
	num_b.setPlaces('3', false);
	num_b.setCurrency(false);
	num_b.setSeparators(false, ',', ',');
	var preis = num_b.toFormatted();
	if( preis < 500 ) { jQuery('#r_preis').val( '500' ); preis = '500'; }
	else if( preis > 15000 ) { jQuery('#r_preis').val( '15000' ); preis = '15000'; }
	var anz = preis / 10;
	zpreis = preis - anz;
	if( zpreis <= 1000 ) {
		if( laufzeit == '18' )
			var zins = 7.26;
		else
			var zins = 5.85;
	} else if( zpreis <= 2500 ) {
		if( laufzeit == '18' )
			var zins = 6.37;
		else
			var zins = 5.01;	
	} else if( zpreis <= 5000 ) {
		if( laufzeit == '18' )
			var zins = 6.10;
		else
			var zins = 4.72;
	} else if( zpreis <= 10000 ) {
		if( laufzeit == '18' )
			var zins = 6.02;
		else
			var zins = 4.46;
	} else if( zpreis <= 15000 ) {
		if( laufzeit == '18' )
			var zins = 5.96;
		else
			var zins = 4.57;
	}
	if( laufzeit == '18' )
		aufschl = 0.27;
	else
		aufschl = 0.20;
	zins = zins + aufschl;
	jQuery('#r_anzahlung').html( runde( anz ) );
	var kpm = runde( (zpreis * zins ) / 100 );
	jQuery('#r_kpm').html( kpm );
	calc();
	return true;
}

function calc_vorteil() {
	var num_b = new NumberFormat();
	num_b.setInputDecimal(',');
	num_b.setNumber( jQuery('#r_v_preis').val() );
	num_b.setPlaces('3', false);
	num_b.setCurrency(false);
	num_b.setSeparators(false, ',', ',');
	var bpreis = num_b.toFormatted();
	var num_b = new NumberFormat();
	num_b.setInputDecimal(',');
	num_b.setNumber( jQuery('#r_leistung').val() );
	num_b.setPlaces('3', false);
	num_b.setCurrency(false);
	num_b.setSeparators(false, ',', ',');
	var leistung = num_b.toFormatted();
	var num_b = new NumberFormat();
	num_b.setInputDecimal(',');
	num_b.setNumber( jQuery('#r_verbrauch').val() );
	num_b.setPlaces('3', false);
	num_b.setCurrency(false);
	num_b.setSeparators(false, ',', ',');
	var verbrauch = num_b.toFormatted();
	var kpm = ( bpreis * leistung * verbrauch ) / 100;
	var kpm_spar = kpm * ( ( 100 - jQuery('#r_ersparnis').val() ) / 100 );
	jQuery('#r_v_kpm').html( runde( kpm ) );
	jQuery('#r_ersparnis_res').html( runde( kpm - kpm_spar ) );
	calc();
	return true;
}

function calc() {
	if( type == 'beides' ) {
		jQuery('#r_leasingkosten').html( jQuery('#r_kpm').html() );
		jQuery('#r_spritersparnis').html( jQuery('#r_ersparnis_res').html() );
		var num_e = new NumberFormat();
		num_e.setInputDecimal(',');
		num_e.setNumber(jQuery('#r_ersparnis_res').html());
		num_e.setPlaces('2', false);
		num_e.setCurrency(false);
		num_e.setSeparators(false, ',', ',');
		var num_k = new NumberFormat();
		num_k.setInputDecimal(',');
		num_k.setNumber(jQuery('#r_kpm').html());
		num_k.setPlaces('2', false);
		num_k.setCurrency(false);
		num_k.setSeparators(false, ',', ',');
		var diff = runde( num_e.toFormatted() - num_k.toFormatted() );
		jQuery('#r_diff').html( '€ ' + diff );
		if( (num_e.toFormatted() - num_k.toFormatted()) > 0 ) {
			jQuery('#r_diff').attr( 'style', 'color: green;' );
			jQuery('.vort').attr( 'style', 'color: green;' );
		} else {
			jQuery('#r_diff').attr( 'style', 'color: red;' ).prepend( '- ' );
			jQuery('.vort').attr( 'style', 'color: red;' );
		}
	}
	return true;
}

function runde(x) {
	var k = (Math.round(x * 100) / 100).toString();
	k += (k.indexOf('.') == -1)? '.00' : '00';
	return formatNumber(k.substring(0, k.indexOf('.') + 3), 2, '.', ',', '', '', '', '' );
	/*return k.substring(0, k.indexOf('.') + 3);*/
}

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {
	var x = Math.round(num * Math.pow(10,dec));
	if (x >= 0) n1=n2='';
	var y = (''+Math.abs(x)).split('');
	var z = y.length - dec;
	
	if (z<0) z--;
	
	for(var i = z; i < 0; i++) 
		y.unshift('0');
	
	y.splice(z, 0, pnt);
	if(y[0] == pnt) y.unshift('0');
	
	while (z > 3) {
		z-=3;
		y.splice(z,0,thou);
	} 
	
	var r = curr1+n1+y.join('')+n2+curr2;
	return r;
}

