
var combodropoffsetY=2; //offset of drop down menu vertically from default location (in px)
var combozindex=100;

if (combodropimage!=""){
	combodropimage='<img class="downimage" src="'+combodropimage+'" title="Select an option" />';
}
function dhtmlselect(selectid){
	var selectbox=document.getElementById(selectid);
	document.write('<div id="dhtml_'+selectid+'" class="dhtmlselect"><span val="0" id="'+selectid+'_val">'+selectbox.title+"</span> "+combodropimage+'<div id="box_'+selectid+'" class="dropdown">');
	for (var i=0; i<selectbox.options.length; i++){
		if (selectid == 'ddprix') {
			document.write('<a href="#" prix="'+selectbox.options[i].value+'">'+selectbox.options[i].text+'</a>');
		}
		else{
			document.write('<a href="#" km="'+selectbox.options[i].value+'">'+selectbox.options[i].text+'</a>');
		}
	}
	document.write('</div></div>');
	selectbox.style.display="none";
	var dhtmlselectbox=document.getElementById("dhtml_"+selectid);
	dhtmlselectbox.style.zIndex=combozindex;
	combozindex--;
	if (typeof selectwidth!="undefined")
		dhtmlselectbox.style.width=selectwidth;
	if (typeof optionwidth!="undefined")
		dhtmlselectbox.getElementsByTagName("div")[0].style.width=optionwidth;
	dhtmlselectbox.getElementsByTagName("div");
	if (combodropimage!="")
		dhtmlselectbox.getElementsByTagName("img")[0];
	dhtmlselectbox.onmouseover=function(){
		this.getElementsByTagName("div")[0].style.display="block";
	};
	dhtmlselectbox.onmouseout=function(){
		this.getElementsByTagName("div")[0].style.display="none";
	};
};

function modif(val, selectid){
	if (selectid == 'ddprix') {
		document.getElementById(selectid+'_val').innerHTML = val + '';
		$('#ddprix_val').attr('val', val);
		document.getElementById('box_ddprix').style.display="none";
	}
	if (selectid == 'ddkm') {
		document.getElementById(selectid+'_val').innerHTML = val + '000';
		$('#ddkm_val').attr('val', val);
		document.getElementById('box_ddkm').style.display="none";
	}
	if ($('#ddprix_val').attr('val') != '0' && $('#ddkm_val').attr('val') != '0') {
		calcul();	
	}
};

function calcul(){
	
	if (window.unit == 'km'){
		var economie = $('#ddkm_val').attr('val') * 1000 * (0.07 *  $('#ddprix_val').attr('val') / 1.4);
		var reduction =  $('#ddkm_val').attr('val') * 1000 * 0.10916667;
		document.getElementById('economie').innerHTML = '$ ' + Math.round(economie/10)*10;
		document.getElementById('reduction').innerHTML = Math.round(reduction)+ ' '+window.masse;
	}
	else{
		var economie = $('#ddkm_val').attr('val') * 1000 *  (0.1008 * $('#ddprix_val').attr('val') / 4.8);
		var reduction =  $('#ddkm_val').attr('val') * 1000 * 0.00019333;
		document.getElementById('economie').innerHTML = 'US$ ' +Math.round(economie/10)*10;
		document.getElementById('reduction').innerHTML = Math.round(reduction*10)/10+ ' '+window.masse;
	}
};

function reset(){
	document.getElementById('ddprix_val').innerHTML = 'Choose';
	document.getElementById('ddkm_val').innerHTML = 'Choose';
	var prix = '';
	var km = '';
	if (window.unit == 'km'){
		prix += '<a href="#" prix="1.40">1,40</a>';
		prix += '<a href="#" prix="1.45">1,45</a>';
		prix += '<a href="#" prix="1.50">1,50</a>';
		prix += '<a href="#" prix="1.55">1,55</a>';
		prix += '<a href="#" prix="1.60">1,60</a>';
		prix += '<a href="#" prix="1.65">1,65</a>';
		prix += '<a href="#" prix="1.70">1,70</a>';
		prix += '<a href="#" prix="1.75">1,75</a>';
		prix += '<a href="#" prix="1.80">1,80</a>';
		
		km += '<a href="#" km="80">80 000</a>';
		km += '<a href="#" km="100">100 000</a>';
		km += '<a href="#" km="120">120 000</a>';
		km += '<a href="#" km="140">140 000</a>';
		km += '<a href="#" km="160">160 000</a>';
		km += '<a href="#" km="180">180 000</a>';
		km += '<a href="#" km="200">200 000</a>';
		km += '<a href="#" km="220">220 000</a>';
		km += '<a href="#" km="240">240 000</a>';
	}
	else{
		prix += '<a href="#" prix="4.60">4.60</a>';
		prix += '<a href="#" prix="4.70">4.70</a>';
		prix += '<a href="#" prix="4.80">4.80</a>';
		prix += '<a href="#" prix="4.90">4.90</a>';
		prix += '<a href="#" prix="5.00">5.00</a>';
		prix += '<a href="#" prix="5.10">5.10</a>';
		prix += '<a href="#" prix="5.20">5.20</a>';
		prix += '<a href="#" prix="5.30">5.30</a>';
		prix += '<a href="#" prix="5.40">5.40</a>';
		
		km += '<a href="#" km="75">75 000</a>';
		km += '<a href="#" km="90">90 000</a>';
		km += '<a href="#" km="105">105 000</a>';
		km += '<a href="#" km="120">120 000</a>';
		km += '<a href="#" km="135">135 000</a>';
		km += '<a href="#" km="150">150 000</a>';
		km += '<a href="#" km="165">165 000</a>';
		km += '<a href="#" km="180">180 000</a>';
		km += '<a href="#" km="195">195 000</a>';
	}
	document.getElementById('box_ddprix').innerHTML = prix;
	document.getElementById('box_ddkm').innerHTML = km;	
	document.getElementById('economie').innerHTML = ''
	document.getElementById('reduction').innerHTML = ''
	$('#ddrpix_val').attr('val','0');
	$('#ddkm_val').attr('val', '0');
	
	$("#box_ddprix > a").click(function() { 
		modif($(this).attr('prix'), 'ddprix');
	});
	
	$("#box_ddkm > a").click(function() { 
		modif($(this).attr('km'), 'ddkm');
	});
	
};

function unit_change(){
	var inputs = document.getElementsByTagName("input");
	
	if (inputs[1].checked == true){
		window.unit = 'mi';
		window.dist = 'Mil';
		window.vol = 'Gallon';
		window.masse = 'Tons';
		document.getElementById('label_unit_prix').innerHTML = 'US$ per Gallon'
		document.getElementById('label_unit_distance').innerHTML = 'Miles per year';
	}
	else{
		window.unit = 'km';
		window.dist = 'km';
		window.vol = 'Litre';
		window.masse = 'kg';
		document.getElementById('label_unit_prix').innerHTML = '$ per litre'
		document.getElementById('label_unit_distance').innerHTML = 'Kilometers per year';
	}
	reset();
};
