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



//** ------------- HB OBJECT -----------------------------
var HB = {
	min_price: 711,
	convert_value: 0.702804,
	
	min_months: 6,
	years: {"1":{"1991":12,"1992":24,"1993":36,"1994":48,"1995":60,"1996":72,"1997":84,"1998":84,"1999":84,"2000":84,"2001":84,"2002":84,"2003":84,"2004":84,"2005":84,"2006":84,"2007":84,"2008":84},"2":{"1991":12,"1992":24,"1993":36,"1994":48,"1995":60,"1996":72,"1997":84,"1998":96,"1999":96,"2000":96,"2001":96,"2002":96,"2003":96,"2004":96,"2005":96,"2006":96,"2007":96,"2008":96}},

	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( ){		
		this.domPrice = document.getElementById('hb_price');
		this.domYear = document.getElementById('hb_year');
		this.domCurrency = document.getElementById('hb_currency');
		this.domFirst = document.getElementById('hb_first');
		this.domType = document.getElementById('hb_type');
		this.domLeft = document.getElementById('hb_left');
		this.domTerm = document.getElementById('hb_term');
		this.domPercents = document.getElementById('hb_percents');
		this.domFirst_rez = document.getElementById('hb_value_first');
		this.domValue_rez = document.getElementById('hb_value');		
		
		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.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;
		
			price = parseFloat( this.domPrice.value );
			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;
			
			price = parseFloat( this.domPrice.value );
			first = parseFloat( this.domFirst.value );
			term = parseFloat( this.domTerm.value );
			percent = parseFloat( this.domPercents.value );
			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 = '';
			for( k in this.years[1] ){
				var option = document.createElement('option');
				option.value = k;
				option.innerHTML = k;
				if( k == val ){ option.selected = true; }
				this.domYear.appendChild( option );
			}
		}
	},
	
	changeYear: function( ){
		if( this.dom == true ){
			var val = this.domTerm.value;
			this.domTerm.innerHTML = '';			
			for( var i = this.min_months; i <= this.years[ this.domType.value ][ this.domYear.value ]; i = i+6 ){
				var option = document.createElement('option');
				option.value = i;
				option.innerHTML = i;
				if( i == val ){ option.selected = true; }
				this.domTerm.appendChild( option );
			}
		}
	}
};


