window.addEvent('domready', function(){

	// Second Example
	var el = $('setAnni'), anni = [0];
	var el2 = $('setTasso'), tasso = [0];
	
	var updateAnni = function(i){
		// Sets the color of the output text and its text to the current color
		el.set('text', i);
		el.dati = i;
		
		calcolaRata();
		
		//el.setStyle('color', color).set('text', i);
	};
	
	var updateTasso = function(i){
		// Sets the color of the output text and its text to the current color
		el2.set('text', i/10);
		
		el2.dati = i/10;
		
		calcolaRata();
		
		//el.setStyle('color', color).set('text', i);
	};
	
	// We call that function to initially set the color output
	updateAnni();
	updateTasso();
	
	
	$$('div.slider.advanced').each(function(el, i){
			
			if(i==0){
											
				var slider = new Slider(el, el.getElement('.knob'), {
					range: [10,50],
					steps: 8,  // Steps from 0 to 255
					wheel: true, // Using the mousewheel is possible too
					onChange: function(){
						// Based on the Slider values set an RGB value in the color array
						
						//alert(i);
						
						anni[i] = this.step;
						// and update the output to the new value
						updateAnni(this.step);
					}
					}).set(15);
			}
			
			if(i==1){
											
				var slider = new Slider(el, el.getElement('.knob'), {
					range: [0,220],
					steps: 220,  // Steps from 0 to 255
					wheel: true, // Using the mousewheel is possible too
					onChange: function(){
						// Based on the Slider values set an RGB value in the color array
						
						//alert(i);
						
						tasso[i] = this.step;
						// and update the output to the new value
						updateTasso(this.step);
					}
					}).set(50);
			}
			
			
			
	});
});

function calcolaRata(){

	var c = document.getElementById("capitale").value;
	var a= document.getElementById("setAnni").dati;
	var ta= document.getElementById("setTasso").dati;
	var pa= document.getElementById("pa").value;
	
	ta = ta / 100;
	
	var fat1 = Math.pow(1+(ta/pa), pa*a);
	
	var primo = c*fat1;
	var fat2 = ta/pa;
	var secondo = fat2/(fat1-1);
	var rata = primo * secondo;
	
	var el3 = $('rata');
	el3.set('text', roundVal(rata));
	
	if(c!=0){
		var el3 = $('numrate');
		el3.set('text', a*pa);
	}
	
}

function roundVal(val){
	var dec = 2;
	var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

