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



//** ------------- HB OBJECT -----------------------------
var HB = {
	min_price: 711,
	convert_value: 0.702804,

	value: null,
	first: null,
	EUR: true,


	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 ){
		var field = document.getElementById( field_id );
		if( field != null ){
			var action = this.EUR == true ? '*' : '/';
			this.EUR = !this.EUR;
			
			eval( 'try{ field.value = this.math_round( parseFloat( field.value )'+ action +'this.convert_value, 0 ); }catch( e ){} ' );
		}
	},
	
	calculate_first: function( ){
		var price = document.getElementById('hb_price');
		var first = document.getElementById('hb_first');
		
		this.first = null;
		
		if( price != null && first != null ){
			price = parseFloat( price.value );
			first = parseFloat( first.value );
			if( price >= this.min_price ){
				this.first = this.math_round( parseFloat( price ) * parseFloat( first ) / 100, 2 );
			}
		}
	},
	
	calculate: function( ){
	
		this.calculate_first();
	
		var price = document.getElementById('hb_price');
		var type = document.getElementById('hb_type');
		var first = document.getElementById('hb_first');
		var term = document.getElementById('hb_term');
		var percent = document.getElementById('hb_percent');
		var left = document.getElementById('hb_left');
		
		this.value = null;
		
		if( price != null && type != null && first != null && term != null && percent != null && left != null ){
			price = parseFloat( price.value );
			first = parseFloat( first.value );
			term = parseFloat( term.value );
			percent = parseFloat( percent.value );
			left = parseFloat( left.value );
			
			if( price >= this.min_price ){ 
				this.value = this.math_round( ((percent / 100 / 12) * ((price - this.first )-((price * ( type.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( ){					
		var value = document.getElementById('hb_value');
		if( value != null ){
			if( this.value != null && !isNaN(this.value) && this.value > 0 ){							
				value.innerHTML = this.value; 
			}else{ value.innerHTML = ''; }												
		}
		
		var first = document.getElementById('hb_value_first');
		if( first != null ){ 
			if( this.first != null && !isNaN(this.first) && this.first > 0  ){
				first.innerHTML = this.first; 
			}else{ first.innerHTML = ''; }
		}
	},
	
	changeType: function( select1 ){
		var select2 = document.getElementById('hb_left');
		if( select1 != null && select2 != null ){
			select2.disabled = ( select1.value != 1 );
			if( select1.value != 1 ){
				select2.selectedIndex = 0;
			}
		}
	}
};


