
//** ------------------------ BODY ON LOAD -----------------------
var hb_tmp_onload = window.onload;
window.onload = function( ){	
	if( typeof(HB) == 'object' ){ 
		if( typeof(_onload) != "undefined" ){
			for( i = 0; i < _onload.length; ++i ){
				if( typeof(_onload[i]) == "function" ){ 
					_onload[i]();
				}else{ 
					eval(_onload[i]+'();'); 
				}
			}
		}
	}
	if( typeof(hb_tmp_onload) == 'function' ){ hb_tmp_onload(); }
}



//** ------------- HB OBJECT -----------------------------
var HB = {

	terms: { 
		1:[3,36,0], // - Sadzīves tehnika 
		2:[3,24,30], // - mobilie telefoni
		3:[3,24,0], // - Ceļojumi un pakalpojumi
		4:[3,36,0], // - Būvmateriāli
		5:[3,36,0], // - Motorolleri, kvadracikli
		6:[3,36,0], // - Iekārtas/instrumenti
		7:[3,36,0], // - Mēbeles
		8:[3,36,0], // - Datortehnika
		9:[3,36,0]  // - Sporta preces
	},	
	term: 1,	
	
	percents: {
			'45-500':{ percent: 25, reward: 1.5 },
			'500.01-1000':{ percent: 23, reward: 1.5 },
			'1000.01-4000':{ percent: 22, reward: 1.5 },
			'4000.01-7000':{ percent: 20, reward: 0.5 },
			min: 45,
			max: 7000,
			find: function( value ){
				value = parseInt(value);
				
				for( k in this ){
					if( k.indexOf('-') == -1 ){ continue; }
					
					var expl = k.split('-');
					if( expl.length == 2 && value >= parseInt(expl[0]) && value <= parseInt(expl[1]) ){
						return this[k];
					}
				}
				
				return false;
			}
	},
	percent_minus:{ 1:1.5, 2:0, 3:3 },
	bcount: 1,
	
///------------------------------------------------------------------------------------------------------------
	
	
	fill_term: function( ){		
		
		var DOM_term = document.getElementById('hb_period');
		var DOM_type = document.getElementById('hb_type');
		
		if( DOM_term != null && DOM_type != null ){
		
			var selected_value = ( DOM_term.selectedIndex == 0 ? null : DOM_term.value );
			DOM_term.innerHTML = '';
			this.term = DOM_type.value;
			
			for( var i = this.terms[DOM_type.value][0]; i <= this.terms[DOM_type.value][1]; ++i ){
				DOM_term.options[DOM_term.options.length] = new Option(i + ' mēneši', i);
				if( selected_value == DOM_term.options[DOM_term.options.length - 1].value ){
					DOM_term.options[DOM_term.options.length - 1].selected = "selected";	
				}
			}
			if( typeof(hbNewSelects) != 'undefined' ){ this.ieFixSelect( 'hb_period' ); }
		}
	},
	
	open_count: function( ){
		var DOM_hb_count = document.getElementById('hb_count');
		if( DOM_hb_count != null ){
			this.bcount = DOM_hb_count.value;
		}
	},
	
	calculate_percents_from_LVL: function( ){
		
		var DOM_hb_price = document.getElementById('hb_price');
		var DOM_hb_first_payment_percent = document.getElementById('hb_first_payment_percent');
		var DOM_hb_first_payment = document.getElementById('hb_first_payment');
		
		if( 
		   DOM_hb_price != null && 
		   DOM_hb_first_payment_percent != null && 
		   DOM_hb_first_payment != null 
		){
			var hb_price = isNaN( parseFloat(DOM_hb_price.value) ) ? 0 : parseFloat(DOM_hb_price.value);
			var hb_first_payment = isNaN( parseFloat(DOM_hb_first_payment.value) ) ? 0 : parseFloat(DOM_hb_first_payment.value);
			
			var sum = hb_price - hb_first_payment;
			
			if( sum >= this.percents.min && sum <= this.percents.max ){
				DOM_hb_first_payment_percent.value = Math.floor(Math.abs((hb_first_payment * 100) / hb_price));
			}
		}
	},

	calculate_percents_from_percents: function( ){
		
		var DOM_hb_price = document.getElementById('hb_price');
		var DOM_hb_first_payment_percent = document.getElementById('hb_first_payment_percent');
		var DOM_hb_first_payment = document.getElementById('hb_first_payment');
		
		if( 
		   	DOM_hb_price != null &&
			DOM_hb_first_payment_percent != null && 
			DOM_hb_first_payment != null 
		){
			var hb_first_payment_percent = isNaN( parseInt(DOM_hb_first_payment_percent.value) ) ? 0 : parseInt(DOM_hb_first_payment_percent.value);
			if( hb_first_payment_percent < 0 ){
				DOM_hb_first_payment_percent.value = 0;
				this.calculate_percents_from_percents();
			}else if( hb_first_payment_percent > 100 ){
				DOM_hb_first_payment_percent.value = DOM_hb_first_payment_percent.value.substr(0,2);
				this.calculate_percents_from_percents();
			}else{
				var hb_price = isNaN( parseFloat(DOM_hb_price.value) ) ? 0 : parseFloat(DOM_hb_price.value);
				var hb_first_payment = Math.ceil(hb_first_payment_percent * hb_price / 100);
				
				var sum = hb_price - hb_first_payment;
				DOM_hb_first_payment.value = hb_first_payment;
				if( sum >= this.percents.min && sum <= this.percents.max ){
					DOM_hb_first_payment.value = hb_first_payment;
				}
			}
		}
	},
	
	calculate: function( calc ){
		
		if( calc != false ){ this.calculate_percents_from_percents(); }
		
		var DOM_hb_value = document.getElementById('hb_value');
		var DOM_hb_price = document.getElementById('hb_price');
		var DOM_hb_first_payment_percent = document.getElementById('hb_first_payment_percent');
		var DOM_hb_first_payment = document.getElementById('hb_first_payment');
		var DOM_hb_period = document.getElementById('hb_period');
		
		if( 
		   	DOM_hb_value != null &&
			DOM_hb_price != null &&
			DOM_hb_first_payment_percent != null && 
			DOM_hb_value != null &&
			DOM_hb_period != null
		){		
			DOM_hb_value.innerHTML = '';
			
			var hb_price = isNaN( parseFloat(DOM_hb_price.value) ) ? 0 : parseFloat(DOM_hb_price.value);						
			var hb_first_payment = isNaN( parseFloat(DOM_hb_first_payment.value) ) ? 0 : parseFloat(DOM_hb_first_payment.value);
			
			var sum = hb_price - hb_first_payment;

			var get_percents = this.percents.find(sum);
			if( get_percents != false ){
				
				var rate = (this.term > 0 && this.terms[this.term][2] > 0 ? this.terms[this.term][2] : get_percents.percent - this.percent_minus[this.bcount] ) / (12*100);				
				if( rate != 0 ){
					var monthpay = sum * Math.pow( (1 + rate), parseInt(DOM_hb_period.value) ) * rate;
					monthpay = monthpay / ( Math.pow((1 + rate), parseInt(DOM_hb_period.value)) - 1 );	
				} 
				else {
					var monthpay = sum/months;
				}
	
				DOM_hb_value.innerHTML = this.math_round( monthpay, 2 );
				
			}
		}
	},



///-------------------------------------------------------------------------------------------------------------------------



/** 
	To FIX/ADD
		1. Add all events to new select FROM old select where they should fire up
**/

	ieFixSelectSelect: function( index ){
		if( typeof(hbNewSelects[select_id]) != "undefined" && typeof(hbNewSelects[select_id].ul) != "undefined" ){
			var real_select = document.getElementById( select_id );
			if( real_select != null ){
				window.this_classname = 'hbNewSelectUlLiHover';
				real_select.selectedIndex = index;
				
				var tmpSpan = hbNewSelects[select_id].span.cloneNode(false);
				hbNewSelects[select_id].div.removeChild( hbNewSelects[select_id].span );
				hbNewSelects[select_id].span = tmpSpan;
				
				for( e = 0; e < hbNewSelects[select_id].ul.childNodes.length; ++e ){
					if( e == index ){ 
						hbNewSelects[select_id].ul.childNodes[e].className = 'hbNewSelectUlLiHover'; 
						hbNewSelects[select_id].span.innerHTML = hbNewSelects[select_id].ul.childNodes[e].innerHTML;
					}else{ hbNewSelects[select_id].ul.childNodes[e].className = ''; }
				}
				hbNewSelects[select_id].div.appendChild( hbNewSelects[select_id].span );
				this.ieFixSelectClose( );
				real_select.onchange();
			}
		}
	},


	ieFixSelectClose: function( ){
		if( typeof(select_id) != "undefined" && select_id != null && typeof(hbNewSelects[select_id]) != "undefined" && typeof(hbNewSelects[select_id].ul) != "undefined" ){
			hbNewSelects[select_id].ul.style.display = 'none';
			hbNewSelects[select_id].div.onmouseup();
		}		
		select_id = null;
	},
	
	
	ieFixSelectOpen: function( select_id ){		
		if( typeof(hbNewSelects[select_id]) != "undefined" && typeof(hbNewSelects[select_id].ul) != "undefined" ){
			if( hbNewSelects[select_id].ul.style.display == 'list-item' ){				
				this.ieFixSelectClose( );
			}else{
				if( typeof(window.select_id) != "undefined" && window.select_id != null ){ this.ieFixSelectClose( ); }
				hbNewSelects[select_id].ul.style.display = 'list-item';
				window.select_id = select_id;
				window.select_just_opened = true;
			}
		}
	},
	

	ieFixSelect: function( id ){		
		var DOM_real_select = typeof(id) == 'object' ? id : document.getElementById( id );
		id = DOM_real_select.id;
		
		if( DOM_real_select != null ){
			
			//Create hbNewSelects array
			if( typeof(window.hbNewSelects) == "undefined" ){ 
			
				window.hbNewSelects = Array();
				this.ieFixSelectsAddStyles();
				
				document.onclick = function( e ){
					if( typeof(select_id) != "undefined" && select_id != null && !window.select_just_opened ){
						HB.ieFixSelectClose( );
					}else if( window.select_just_opened ){
						window.select_just_opened = false;	
					}
				}
				
				document.onmouseup = function( e ){ 
					if( typeof(select_id) != "undefined" && select_id != null ){
						hbNewSelects[select_id].div.onmouseup();					
					}
				}
				
				//window.onblur = function( e ){
					//HB.ieFixSelectClose();
				//}
			}

			//Remove newSelect from DOM if already exists
			if( typeof(hbNewSelects[id]) != "undefined" ){ 
				hbNewSelects[id].div.parentNode.removeChild(hbNewSelects[id].div); 
				
				// Some strange fix for IE
				DOM_real_select.style.display = 'block';
			}


			//Create new select object
			hbNewSelects[id] = Object();
			hbNewSelects[id].around_div = document.createElement('div');
			hbNewSelects[id].around_div.style.cssText = 'float: left;';
			
			hbNewSelects[id].div = document.createElement('div');
			hbNewSelects[id].div.id = 'hbNewSelect' + id;
			hbNewSelects[id].div.className = 'hbNewSelect';
			
			hbNewSelects[id].div.position1 = Math.floor( ( (DOM_real_select.offsetHeight - 2) / 2 ) ) - ( 6 );
			hbNewSelects[id].div.position2 = 23 - hbNewSelects[id].div.position1;
			hbNewSelects[id].div.position3 = 46 - hbNewSelects[id].div.position1;
			
			hbNewSelects[id].div.style.cssText = 
				' background-position: right '+ hbNewSelects[id].div.position1 +'px; ' + 
				' height: '+ (DOM_real_select.offsetHeight - 2) + 'px; width: '+ (DOM_real_select.offsetWidth - 2) + 'px';
			
			hbNewSelects[id].div.onselectstart = function( ){ return false; }
			eval(
				" hbNewSelects['"+ id +"'].div.onmousedown = function( e ){ " + 
					" e = ( typeof(window.event) != 'undefined' ? window.event.srcElement : e.target ); " + 
					" if( (e.className == 'hbNewSelect' || e.className == 'hbNewSelectSpan') && (typeof(window.select"+ id +") == 'undefined' || window.select"+ id +" == false) ){ " +
						" this.style.backgroundPosition = 'right -"+ hbNewSelects[id].div.position3 +"px'; " + 
						" window.select"+ id +" = true; HB.ieFixSelectOpen('"+ id +"'); " + 
					" }" +
				"} "
			);
			eval(" hbNewSelects['"+ id +"'].div.onmouseup = function( ){ this.style.backgroundPosition = 'right "+ hbNewSelects[id].div.position1 +"px'; window.select"+ id +" = false; } ");			
			eval(" hbNewSelects['"+ id +"'].div.onmouseout = function( ){ if( typeof(window.select"+ id +") == 'undefined' || window.select"+ id +" == false ){ this.style.backgroundPosition = 'right "+ hbNewSelects[id].div.position1 +"px'; } }");
			eval(
				" hbNewSelects['"+ id +"'].div.onmousemove = function( e ){ " + 
					" e = ( typeof(window.event) != 'undefined' ? window.event.srcElement : e.target ); " + 
					" if( (e.className == 'hbNewSelect' || e.className == 'hbNewSelectSpan') && (typeof(window.select"+ id +") == 'undefined' || window.select"+ id +" == false) ){ " +
						" this.style.backgroundPosition = 'right -"+ hbNewSelects[id].div.position2 +"px' " + 
					" }" +
				"} "
			);
			hbNewSelects[id].around_div.appendChild( hbNewSelects[id].div );
			
			
			// SPAN
			hbNewSelects[id].span = document.createElement('span');
			hbNewSelects[id].span.className = 'hbNewSelectSpan';
			hbNewSelects[id].span.style.cssText = ' height: '+ (DOM_real_select.offsetHeight - 4) + 'px; width: '+ (DOM_real_select.offsetWidth - 2 - 19) + 'px; ';
			hbNewSelects[id].div.appendChild( hbNewSelects[id].span );
			
			

			//UL			
			hbNewSelects[id].ul = document.createElement('ul');
			hbNewSelects[id].ul.id = 'hb_selectDropDown' + i;
			hbNewSelects[id].ul.className = 'hbNewSelectUl';
			hbNewSelects[id].ul.onselectstart = function( ){ return false; }
			var addon = ( navigator.userAgent.toLowerCase().indexOf('msie 6') != -1 ? ' width: 100%; ' : '');
			hbNewSelects[id].ul.style.cssText = addon + ' min-width: '+ hbNewSelects[id].div.style.width +';';
			
			
			for( e = 0; e < DOM_real_select.options.length; ++e ){
				var li = document.createElement('li');
				if( e == DOM_real_select.selectedIndex ){ 
					li.className = 'hbNewSelectUlLiHover';
					hbNewSelects[id].span.innerHTML = DOM_real_select.options[DOM_real_select.selectedIndex].innerHTML;
				}
				li.innerHTML = DOM_real_select.options[e].innerHTML;
				eval(" li.onclick = function( ){ HB.ieFixSelectSelect( "+ e +" ); } ");
				eval(" li.onmouseover = function( ){ window.this_classname = (this.className == 'hbNewSelectUlLiHover'); this.className = 'hbNewSelectUlLiHover'; } ");
				eval(" li.onmouseout = function( ){ if( !this_classname ){ this.className = ''; } } ");
				eval(" li.onselectstart = function( ){ return false; } " );
				hbNewSelects[id].ul.appendChild( li );
			}
			
			//if( e >= 20 ){
				//hbNewSelects[id].ul.style.cssText += ' overflow-y: scroll; height: 272px; ';
			//}
			hbNewSelects[id].around_div.appendChild( hbNewSelects[id].ul );
			//hbNewSelects[id].div.appendChild( hbNewSelects[id].ul );
			
			
			
			// Hide existing select
			DOM_real_select.style.display = 'none';
			
			// Put new select in DOM
			DOM_real_select.parentNode.insertBefore( hbNewSelects[id].around_div, DOM_real_select );
			
			//Fix IE 6
			if( parseInt(hbNewSelects[id].div.style.height) == hbNewSelects[id].div.offsetHeight ){ hbNewSelects[id].div.style.height = ( parseInt(hbNewSelects[id].div.style.height) + 2 ) + 'px'; }
			if( hbNewSelects[id].div.offsetWidth > hbNewSelects[id].ul.offsetWidth ){ hbNewSelects[id].ul.style.minWidth = (hbNewSelects[id].div.offsetWidth-2) + 'px'; }
		}
	},
	
	ieFixSelectsAddStyles: function( ){
		this.styleAddRule( '.hbNewSelect', 
			' cursor: default; background: #FFF url(hb-nomaksa_dropdown.png) no-repeat; ' + 
			' border: 1px #A4B97F solid; '
		);
		
		this.styleAddRule( '.hbNewSelectSpan', 
			' color: #004366; display: block; float: left; overflow: hidden; white-space: nowrap;' + 
			' text-overflow: ellipsis; padding: 1px 1px 0px 1px; '
		);
		
		this.styleAddRule( '.hbNewSelectClearP', 'height: 0; display: block; visibility: hidden; overflow: hidden; clear: both;' );
		
		this.styleAddRule( '.hbNewSelectUl', 
			' border: 1px #A4B97F solid; margin-left: -1px; position: absolute; background: #FFF; color: #004366; z-index: 1000; ' + 
			' list-style: none; display: none; '
		);
		
		this.styleAddRule( '.hbNewSelectUl li', 
			' cursor: default; border: 1px #FFF solid; padding: 0px 3px 0px 2px; white-space: nowrap; '
		);
		this.styleAddRule( '.hbNewSelectUlLiHover', 
			' border: 1px solid #666666; background: #A4B97F; color: #FFFFFF; '
		);
	},
	
	ieFixSelects: function( ){
		
		var hb_form = document.getElementById('hb_form');		
		
		if( hb_form != null ){
			var selects = hb_form.getElementsByTagName('select');	
			for( i = 0; i < selects.length; ++i ){
				if( selects[i].style.display != 'none' ){ this.ieFixSelect( selects[i] ); }
			}
		}
	},
	

/// ----------------------------------------------------------------------------------------------------------------
	
	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];
		
	},
	
	styleAddRule: function( rule_name, rule ){
		var styleSheet = document.styleSheets[document.styleSheets.length - 1];
		
		if( styleSheet.addRule ){ styleSheet.addRule( rule_name, rule ) }
		else if( styleSheet.insertRule ){ styleSheet.insertRule( rule_name +'{'+ rule +'}', styleSheet.cssRules.length-1 ); }
	}
	
};


