﻿function Select_Modele(type) {
    // on deselectionne le dernier choisie
    if ($('modele_select').value != '' && $('modele_select').value != type) {
        new Effect.Opacity('search_modele_'+$('modele_select').value, { from: 0, to: 1, duration: 0.2 });    
    }
    
    $('modele_select').value = type;
    
    // on met à jour la liste des motorisations
    Load_Motorisation();
}

function Over_Modele(type) {
    if ($('modele_select')) {
        if (type.id != 'search_modele_'+$('modele_select').value) new Effect.Opacity(type, { from: 1, to: 0, duration: 0.2 });
    }
}

function Out_Modele(type) {
    if (type.id != 'search_modele_'+$('modele_select').value) new Effect.Opacity(type, { from: 0, to: 1, duration: 0.2 });
}


function Load_Slider() {

    var now = new Date()

    var handles = [$('slider_km_handle_min'), $('slider_km_handle_max')];
    var square_slider = new Control.Slider2(handles, 'slider_km', {
        range:$R(0, 150000, false)
        , step:1
        , restricted:true
        , increment:1
        , sliderValue: [0, 150000]
        , onSlide: function(values) {
            // Valeur affichée
            $('slider_km_min').innerHTML = formatnumjs((Math.round(values[0]/10000))*10000) + ' km';
            $('slider_km_max').innerHTML = formatnumjs((Math.round(values[1]/10000))*10000) + ' km';
            // Valeur search
            $('km_min').value = (Math.round(values[0]/10000)*10000);
            $('km_max').value = (Math.round(values[1]/10000)*10000);
        }
    }, 'slider_km_track');
    
    var handles = [$('slider_year_handle_min'), $('slider_year_handle_max')];
    var square_slider = new Control.Slider2(handles, 'slider_year', {
    range: $R(2001, parseInt(now.getFullYear()), false)
        , step:1
        , restricted:true
        , increment:1
        , sliderValue: [2001, parseInt(now.getFullYear())]
        , onSlide: function(values) {
            // Valeur affichée
            $('slider_year_min').innerHTML = Math.round(values[0]);
            $('slider_year_max').innerHTML = Math.round(values[1]);
            // Valeur search
            $('year_min').value = Math.round(values[0]);
            $('year_max').value = Math.round(values[1]);
        }
    }, 'slider_year_track');
    
    var handles = [$('slider_price_handle_min'), $('slider_price_handle_max')];
    var square_slider = new Control.Slider2(handles, 'slider_price', {
        range:$R(2000, 50000, false)
        , step:1
        , restricted:true
        , increment:1
        , sliderValue: [2000, 50000]
        , onSlide: function(values) {
            // Valeur affichée
            $('slider_price_min').innerHTML = formatnumjs(Math.round(values[0]/1000)*1000)+' &euro;';
            $('slider_price_max').innerHTML = formatnumjs(Math.round(values[1]/1000)*1000)+' &euro;';
            // Valeur search
            $('price_min').value = (Math.round(values[0]/1000)*1000);
            $('price_max').value = (Math.round(values[1]/1000)*1000);
        }
    }, 'slider_price_track');
}



function View_Financement() {
    
    // On calcul les mensulaité et le taux
    vpmBMW.set();
    // on calcul la dete de validité
    var datevalid = new Date();
    datevalid = datevalid.setMonth(datevalid.getMonth() + 1);
    $('datevalid').innerHTML = dateFormat(datevalid, "dd/mm/yyyy");
    $('datevalid2').innerHTML = dateFormat(datevalid, "dd/mm/yyyy");
    
    $('finan_modele').innerHTML = $('td_present_modele').innerHTML;

    $('Calcul_Financement').style.display = 'none';

    Effect.toggle('Calcul_Financement', 'blind', { duration: 2.0 });
}

/*
######## VPM
*/
var vpmBMW = {
	constanteDeN : 0.31090128755364806866952789699571,
	mensualites : 12,
	teg : [],
	pourcentage : function() {
		var taux = 10.95;
		return taux - (taux* (this.constanteDeN/(this.mensualites/12))/100);
        //return taux - (taux* this.constanteDeN /100);
	},
	get : function(mensualite, emprunt){
		this.mensualites = mensualite;
		var t_mensuel = (this.pourcentage()/12)/100;
		var R = ( 1 - Math.pow( (1+t_mensuel), -mensualite) ) / t_mensuel;
		var VPM = ((emprunt)/R);
		return VPM;
	},
	set : function(){
//		this.teg[12] = { teg:'11.52', taeg:'12.15'};
//        this.teg[13] = { teg:'11.39', taeg:'12.00'};
//        this.teg[14] = { teg:'11.27', taeg:'11.87'};
//        this.teg[15] = { teg:'11.17', taeg:'11.76'};
//        this.teg[16] = { teg:'11.09', taeg:'11.67'};
//        this.teg[17] = { teg:'11.01', taeg:'11.58'};
//        this.teg[18] = { teg:'10.94', taeg:'11.50'};
//        this.teg[19] = { teg:'10.88', taeg:'11.43'};
//        this.teg[20] = { teg:'10.82', taeg:'11.37'};
//        this.teg[21] = { teg:'10.77', taeg:'11.32'};
//        this.teg[22] = { teg:'10.72', taeg:'11.26'};
//        this.teg[23] = { teg:'10.68', taeg:'11.22'};
//        this.teg[24] = { teg:'10.64', taeg:'11.17'};
//        this.teg[25] = { teg:'10.60', taeg:'11.13'};
//        this.teg[26] = { teg:'10.57', taeg:'11.09'};
//        this.teg[27] = { teg:'10.54', taeg:'11.06'};
//        this.teg[28] = { teg:'10.51', taeg:'11.03'};
//        this.teg[29] = { teg:'10.48', taeg:'11.00'};
//        this.teg[30] = { teg:'10.45', taeg:'10.97'};
//        this.teg[31] = { teg:'10.43', taeg:'10.94'};
//        this.teg[32] = { teg:'10.41', taeg:'10.92'};
//        this.teg[33] = { teg:'10.39', taeg:'10.89'};
//        this.teg[34] = { teg:'10.37', taeg:'10.87'};
//        this.teg[35] = { teg:'10.35', taeg:'10.85'};
//        this.teg[36] = { teg:'10.33', taeg:'10.83'};
//        this.teg[37] = { teg:'10.31', taeg:'10.81'};
//        this.teg[38] = { teg:'10.30', taeg:'10.80'};
//        this.teg[39] = { teg:'10.28', taeg:'10.78'};
//        this.teg[40] = { teg:'10.27', taeg:'10.76'};
//        this.teg[41] = { teg:'10.25', taeg:'10.75'};
//        this.teg[42] = { teg:'10.24', taeg:'10.73'};
//        this.teg[43] = { teg:'10.23', taeg:'10.72'};
//        this.teg[44] = { teg:'10.21', taeg:'10.71'};
//        this.teg[45] = { teg:'10.20', taeg:'10.69'};
//        this.teg[46] = { teg:'10.19', taeg:'10.68'};
//        this.teg[47] = { teg:'10.18', taeg:'10.67'};
//        this.teg[48] = { teg:'10.17', taeg:'10.66'};
//        this.teg[49] = { teg:'10.16', taeg:'10.65'};
//        this.teg[50] = { teg:'10.15', taeg:'10.64'};
//        this.teg[51] = { teg:'10.14', taeg:'10.63'};
//        this.teg[52] = { teg:'10.13', taeg:'10.62'};
//        this.teg[53] = { teg:'10.13', taeg:'10.61'};
//        this.teg[54] = { teg:'10.12', taeg:'10.60'};
//        this.teg[55] = { teg:'10.11', taeg:'10.59'};
//        this.teg[56] = { teg:'10.10', taeg:'10.58'};
//        this.teg[57] = { teg:'10.10', taeg:'10.58'};
//        this.teg[58] = { teg:'10.09', taeg:'10.57'};
//        this.teg[59] = { teg:'10.08', taeg:'10.56'};
//        this.teg[60] = { teg:'10.08', taeg:'10.55'};
this.teg[12] = { teg:'12,82', taeg:'13,60'};
this.teg[13] = { teg:'12,69', taeg:'13,46'};
this.teg[14] = { teg:'12,58', taeg:'13,33'};
this.teg[15] = { teg:'12,48', taeg:'13,22'};
this.teg[16] = { teg:'12,39', taeg:'13,12'};
this.teg[17] = { teg:'12,31', taeg:'13,03'};
this.teg[18] = { teg:'12,24', taeg:'12,95'};
this.teg[19] = { teg:'12,18', taeg:'12,88'};
this.teg[20] = { teg:'12,12', taeg:'12,82'};
this.teg[21] = { teg:'12,07', taeg:'12,76'};
this.teg[22] = { teg:'12,02', taeg:'12,71'};
this.teg[23] = { teg:'11,98', taeg:'12,66'};
this.teg[24] = { teg:'11,94', taeg:'12,62'};
this.teg[25] = { teg:'11,91', taeg:'12,58'};
this.teg[26] = { teg:'11,87', taeg:'12,54'};
this.teg[27] = { teg:'11,84', taeg:'12,50'};
this.teg[28] = { teg:'11,81', taeg:'12,47'};
this.teg[29] = { teg:'11,78', taeg:'12,44'};
this.teg[30] = { teg:'11,76', taeg:'12,41'};
this.teg[31] = { teg:'11,73', taeg:'12,39'};
this.teg[32] = { teg:'11,71', taeg:'12,36'};
this.teg[33] = { teg:'11,69', taeg:'12,34'};
this.teg[34] = { teg:'11,67', taeg:'12,31'};
this.teg[35] = { teg:'11,65', taeg:'12,29'};
this.teg[36] = { teg:'11,63', taeg:'12,27'};
this.teg[37] = { teg:'11,62', taeg:'12,25'};
this.teg[38] = { teg:'11,60', taeg:'12,24'};
this.teg[39] = { teg:'11,58', taeg:'12,22'};
this.teg[40] = { teg:'11,57', taeg:'12,20'};
this.teg[41] = { teg:'11,56', taeg:'12,19'};
this.teg[42] = { teg:'11,54', taeg:'12,17'};
this.teg[43] = { teg:'11,53', taeg:'12,16'};
this.teg[44] = { teg:'11,52', taeg:'12,15'};
this.teg[45] = { teg:'11,51', taeg:'12,13'};
this.teg[46] = { teg:'11,50', taeg:'12,12'};
this.teg[47] = { teg:'11,49', taeg:'12,11'};
this.teg[48] = { teg:'11,48', taeg:'12,10'};
this.teg[49] = { teg:'11,47', taeg:'12,09'};
this.teg[50] = { teg:'11,46', taeg:'12,08'};
this.teg[51] = { teg:'11,45', taeg:'12,07'};
this.teg[52] = { teg:'11,44', taeg:'12,06'};
this.teg[53] = { teg:'11,43', taeg:'12,05'};
this.teg[54] = { teg:'11,42', taeg:'12,04'};
this.teg[55] = { teg:'11,42', taeg:'12,03'};
this.teg[56] = { teg:'11,41', taeg:'12,02'};
this.teg[57] = { teg:'11,40', taeg:'12,02'};
this.teg[58] = { teg:'11,39', taeg:'12,01'};
this.teg[59] = { teg:'11,39', taeg:'12,00'};
this.teg[60] = { teg:'11,38', taeg:'11,99'};




		var mensualite = $('duree_value').value;
		
		var prix_veh = $('prix_value').value;
		var apport_value = $('apport_value').value;
		
		var emprunt = parseFloat(prix_veh) - parseFloat(apport_value);

		$("apportperso").innerHTML = $("apport_value").value;
		$("finan_prix_veh").innerHTML = prix_veh;		
		$("emprunt").innerHTML = emprunt;

		var cout_mensuel = (Math.round( (this.get(mensualite, emprunt+this.get(1, emprunt/100))*100) ) /100);

		$("montantmensualite").innerHTML = cout_mensuel;
		$("montantmensualite2").innerHTML = cout_mensuel;
		$("mensualite").innerHTML = mensualite;
		
		$("teg1").innerHTML = this.teg[mensualite].teg;
		$("teg2").innerHTML = this.teg[mensualite].taeg;
	
		$("fraisdos").innerHTML	= (emprunt/100);
		$("credit").innerHTML = (Math.round( ((cout_mensuel * mensualite)-emprunt)*100) /100);
		$("totalcredit").innerHTML = ((Math.round((cout_mensuel * mensualite)*100))/100);
		
	}
}



function Load_Financement() {
    
    var apport_max = (Math.round(parseInt($('prix_value').value)/100)*75);
    
    var dep_apport = $('apport_value').value;
    
    // horizontal slider control
    var sliderAp = new Control.Slider('apport_slider_handle', 'apport_slider', {
        range: $R(0, apport_max),
        sliderValue: [dep_apport, apport_max],
        onSlide: function(v) {
        $('result_apport').innerHTML = ((Math.round(v.toFixed()/100)) * 100)+" &euro;";
        $('result_apport').style.left = ( ( (v.toFixed()*(240-17))/30100 ) )+"px";
        $('apport_value').value = ((Math.round(v.toFixed()/100)) * 100);
        }
    }, '');
    
    var dep_duree = $('duree_value').value;
    
    var square_slider = new Control.Slider('duree_slider_handle', 'duree_slider', {
        range: $R(12, 60, false),
        values: $R(12, 60),
        sliderValue: [dep_duree, 60],
        step: 1,
        onSlide: function(values) {
            $('result_duree').innerHTML=formatnumjs(values)+' mois';
            $('duree_value').value=values;
        },
        onChange: function(values) { 
            $('result_duree').innerHTML=formatnumjs(values)+' mois';
            $('duree_value').value=values;
            
        }
    });
}



function mouseOut(elmt) {
    $(elmt).src = '/vomini/commun/img/pixel_trans.gif';
}

function mouseOver(elmt) {
    $(elmt).src = '/vomini/commun/img/arrow.gif';
}

function hideLayer(layerName) {    
	if ($(layerName)) {
		$(layerName).style.visibility = 'hidden';
	}
}

function showLayer(layerName) {
	if ($(layerName)) {
		$(layerName).style.visibility = 'visible';
	}
}



