
//** ------------------------ BODY ON LOAD -----------------------
window.onload = function( ){
	if(typeof(SK)!="undefined"){SK.init();}
}



//** ------------- Swedbank Kalkulatora objekts -----------------------------
var SK = {
	min_price: 711,
	convert_value: 0.702804,
	
	min_months: 6,
	years: {"1":60, "2": 84},

	value: null,
	first: null,
	EUR: true,
	
	//^ ---- fields ---
	dom: false,
	domPrice: null,
	domYear: null,
	domCurrency: null,
	domFirst: null,
	domType: null,
	domLeft: null,
	domTerm: null,
	domPercents: null,
	domFirst_rez: null,
	domValue_rez: null,
	//^ ---- fields ---
	
	init : function( ){
	
        // Get all dom objects
		this.domPrice = document.getElementById('sk_price');
		this.domYear = document.getElementById('sk_year');
		this.domCurrency = document.getElementById('sk_currency');
		this.domFirst = document.getElementById('sk_first');
		this.domType = document.getElementById('sk_type');
		this.domLeft = document.getElementById('sk_left');
		this.domTerm = document.getElementById('sk_term');
		this.domPercents = document.getElementById('sk_percents');
		this.domFirst_rez = document.getElementById('sk_first_payment');
		this.domValue_rez = document.getElementById('sk_monthly_payment');		           


		if( 
		   	this.domPrice != null && this.domYear != null && this.domCurrency != null && this.domFirst != null && this.domType != null && 
		    this.domLeft != null && this.domTerm != null && this.domPercents != null && this.domFirst_rez != null && this.domValue_rez != null 
		){
			this.dom = true;
			
			this.fillYears();
			this.domYear.value = document.getElementById('skSelYear').value;
			this.changeYear();
			this.calculate();
		}
	},


	math_round: function( number, after ){					
		var tmp = Math.round( number * Math.pow(10, after) ) / Math.pow(10, after);
		var tmp = tmp.toString().split( '.' );
		tmp[1] = ( typeof(tmp[1]) != 'undefined' ? tmp[1] : 0 ) + new Array( 2 - ( typeof(tmp[1]) != 'undefined' ? tmp[1].length : +1 ) + 1 ).join('0'); 
		
		
		return after > 0 && tmp[1] ? tmp.join('.') : tmp[0];
		
	},
	
	convert: function( field_id ){
		if( this.dom == true && this[ field_id ] ){
			var action = this.EUR == true ? '*' : '/';
			this.EUR = !this.EUR;
			
			eval( 'try{ this[ field_id ].value = this.math_round( parseFloat( this[ field_id ].value )'+ action +'this.convert_value, 0 ); }catch( e ){} ' );
		}
	},
		
	calculate_first: function( ){
		if( this.dom == true ){
			this.first = null;
			var price = parseFloat( this.domPrice.value ); // ERROR
			var first = parseFloat( this.domFirst.value );
			if( price >= this.min_price ){
				this.first = this.math_round( parseFloat( price ) * parseFloat( first ) / 100, 2 );
			}
		}
	},
	
	calculate: function( ){
	
		this.calculate_first();
	
		if( this.dom == true ){
			this.value = null;
			
			var price = parseFloat( this.domPrice.value );
			var first = parseFloat( this.domFirst.value );
			var term = parseFloat( this.domTerm.value );
			var percent = parseFloat( this.domPercents.value );
			var left = parseFloat( this.domLeft.value );
			
			if( price >= this.min_price ){ 
				this.value = this.math_round( ((percent / 100 / 12) * ((price - this.first )-((price * ( this.domType.value == 1 ? left : 0 )/*atlikusī vērtība*/ / 100) / (Math.pow((percent / 100 / 12) + 1, term)))) / (1 - (1 / Math.pow((percent / 100 / 12) + 1, term)))), 2 );
			}
				
			this.show( );
		}
	},
	
	show: function( ){
		if( this.dom == true ){
			if( this.value != null && !isNaN(this.value) && this.value > 0 ){							
				this.domValue_rez.innerHTML = this.value; 
			}else{ this.domValue_rez.innerHTML = ''; }												
		
			if( this.first != null && !isNaN(this.first) && this.first > 0  ){
				this.domFirst_rez.innerHTML = this.first; 
			}else{ this.domFirst_rez.innerHTML = ''; }
		}
	},
	
	changeType: function( ){
		if( this.dom == true ){
			this.domLeft.disabled = ( this.domType.value != 1 );
			if( this.domType.value != 1 ){
				this.domLeft.selectedIndex = 0;
			}
			
			this.changeYear( );
		}
	},
	
	fillYears: function( ){
		if( this.dom == true ){
			var val = this.domYear.value;
			this.domYear.innerHTML = '';
			
			var date = new Date();
            var cyear = date.getFullYear();

			for (i = cyear - 15; i <= cyear; ++i)
			{
				var option = document.createElement('option');
				option.value = i;
				option.innerHTML = i;
				if( i == val ){ option.selected = true; }
				this.domYear.appendChild( option );
			}
		}
	},
	
	changeYear: function( ){
		if( this.dom == true ){
			var val = this.domTerm.value;
			this.domTerm.innerHTML = '';			
			
			
			// Set date
            var date = new Date();
            var cyear = date.getFullYear();

			var max = (16 - (cyear - this.domYear.value)) * 12;
			if (max > this.years[this.domType.value])
			{
                max = this.years[this.domType.value];
			}
			
			for( var i = this.min_months; i <= max; i = i+6 ){
				var option = document.createElement('option');
				option.value = i;
				option.innerHTML = i;
				if( i == val ){ option.selected = true; }
				this.domTerm.appendChild( option );
			}
		}
	}
};

