/*
<div style="margin-left: 10px;">

<div style="margin-bottom: 20px;">Kwota kredytu (netto)</div>
<div id="kredyt" style="width: 60%; float: left; margin-right: 40px;"></div>
<input type="text" id="kredyt-wartosc" style="font-size: 12px; text-align: right;"/> PLN

<br clear="all"/>
<div style="float:right; margin-right: 105px; color: #ff0000; margin-top: 5px;" id="kredyt-error"></div>


<br clear="all"/>

<div style="margin-bottom: 20px;">Ilość rat</div>

<div id="raty" style="width: 60%; float: left; margin-right: 40px;"></div>
<input type="text" id="raty-wartosc" style="font-size: 12px; text-align: right;"/>

<br clear="all"/>
<div style="float:right; margin-right: 105px; color: #ff0000; margin-top: 5px;" id="raty-error"></div>

<br clear="all"/>

<div style="float: right; font-size: 1.2em; margin-right: 105px;">
Wysokość raty&nbsp;:&nbsp;<span id="rata" style="font-size: 1.5em;"></span>
</div>

<br clear="all"/>

</div>
*/

/*

KONFIGURACJA DO SĹOWNIKA
 
var DEFAULT_ILOSC_RAT = 12;
var MIN_ILOSC_RAT = 6;
var MAX_ILOSC_RAT = 96;

var DEFAULT_KWOTA = 4000;
var MIN_KWOTA = 2000;
var MAX_KWOTA = 500000;

var URD = 1.1;

// [ilosc_rat_min, ilosc_rat_max, oprocentowanie]
var PROGI = [
    [6, 48, 8.99],
    [49, 84, 9.99],
    [85, 96, 10.49]
];

*/

//<script type="text/javascript" src="js/kalkulator-kredyt-gotowkowy.js"></script>
/*
document.write(
 
'<div style="margin-left: 10px;">' + 
'<div style="margin-bottom: 20px;">Kwota kredytu (netto)</div>' +
'<div id="kredyt" style="width: 60%; float: left; margin-right: 40px;"></div>' +
'<input type="text" id="kredyt-wartosc" style="font-size: 12px; text-align: right;"/> zł' +
'<br clear="all"/>' +
'<div style="float:right; margin-right: 105px; color: #ff0000; margin-top: 5px;" id="kredyt-error">&nbsp;</div>' +
'<br clear="all"/>' +
'<div style="margin-bottom: 20px;">Liczba rat</div>' +
'<div id="raty" style="width: 60%; float: left; margin-right: 40px;"></div>' +
'<input type="text" id="raty-wartosc" style="font-size: 12px; text-align: right;"/>' +
'<br clear="all"/>' +
'<div style="float:right; margin-right: 105px; color: #ff0000; margin-top: 5px;" id="raty-error">&nbsp;</div>' +
'<br clear="all"/>' +
'<div style="float: right; font-size: 1.2em; margin-right: 105px;">' +
'Wysokość raty&nbsp;:&nbsp;<span id="rata" style="font-size: 1.5em;"></span>' +
'</div>' +
'<br clear="all"/>' +
'</div>'
);

*/

$(function() {
		$( "#kredyt" ).slider({
			range: "min",
			value: DEFAULT_KWOTA,
			min: MIN_KWOTA,
			max: MAX_KWOTA,
			step: 500,
			slide: function( event, ui ) {
				$('#kredyt-error').html('&nbsp;');
				$("#kredyt-wartosc").val( addSpaces(ui.value) );
				policzRate();
			}
		});
		
		$( "#kredyt-wartosc" ).val( $( "#kredyt" ).slider( "value" ) );
		
		$( "#raty" ).slider({
			range: "min",
			value: DEFAULT_ILOSC_RAT,
			min: MIN_ILOSC_RAT,
			max: MAX_ILOSC_RAT,
			step: 1,
			slide: function( event, ui ) {
				$('#raty-error').html('&nbsp;');
				$( "#raty-wartosc" ).val( ui.value );
				policzRate();
			}
		});
		
		$( "#raty-wartosc" ).val( $( "#raty" ).slider( "value" ) );
		
		$( "#raty-wartosc" ).keyup(function(){			
			var value = $(this).val();			
			if(!isNaN(value)){
				$("#raty").slider('value', value);
			}
		});
		
		$("#kredyt-wartosc").keydown(function (event) {
			if ((!event.shiftKey && !event.ctrlKey && !event.altKey) && ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105))) // 0-9 or numpad 0-9, disallow shift, ctrl, and alt
			{
			// check textbox value now and tab over if necessary
			}
			else if (event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39) // not esc, del, left or right
			{
				event.preventDefault();
			}
			// else the key should be handled normally
		}); 
		
		$( "#kredyt-wartosc" ).keyup(function(){			
			var value = $(this).val().split(' ').join('');;	
			
			$('#kredyt-error').html('&nbsp;');
			
			if(!isNaN(value)){
				
				if( value > MAX_KWOTA ){
					value = MAX_KWOTA;
					$('#kredyt-error').html('Kwota kredytu nie może być większa niż ' + MAX_KWOTA);
				}
				
				if( value < MIN_KWOTA ){
					value = MIN_KWOTA;
					$('#kredyt-error').html('Kwota kredytu nie może być mniejsza niż ' + MIN_KWOTA);
				}
				
				$("#kredyt").slider('value', value);
				
				policzRate();
			}else{
				$('#kredyt-error').html('Niepoprawna wartość');
			}
		});
		
		$( "#raty-wartosc" ).keyup(function(){	
			
			var value = $(this).val().split(' ').join('');	
			
			$('#raty-error').html('&nbsp;');
			
			if(!isNaN(value)){
				
				if( value > MAX_ILOSC_RAT ){
					value = MAX_ILOSC_RAT;
					
					$('#raty-error').html('Liczba rat nie może być większa niż ' + MAX_ILOSC_RAT);
					
					//$(this).val(value);
				}
				
				if( value < MIN_ILOSC_RAT ){
					value = MIN_ILOSC_RAT;
					
					$('#raty-error').html('Liczba rat nie może być mniejsza niż ' + MIN_ILOSC_RAT);					
				}
				
				$("#raty").slider('value', value);
				
				policzRate();
			}else{
				$('#raty-error').html('Niepoprawna wartość');
			}
			
		});
		
		policzRate();
		
		function policzRate(){
						
			var kredyt = $("#kredyt-wartosc").val().split(' ').join('');
			var raty = $("#raty-wartosc").val().split(' ').join('');
			
			if( isNaN(kredyt) || isNaN(raty) ){
				$('#rata').html(
					"?"
				);
				return;
			}
			
			if( kredyt < MIN_KWOTA || kredyt > MAX_KWOTA ){
				$('#rata').html(
					"?"
				);
				return;
			}
			
			if( raty < MIN_ILOSC_RAT || raty > MAX_ILOSC_RAT){
				$('#rata').html(
					"?"
				);
				return;
			}
			
			
			
			var opr = 0;
			var potega = prowizja+URD*raty;
			
				if (kredyt>=76400){
					var potega = prowizja+URD2*raty;
					}
			
			var mnoznik = Math.pow(potega,7)+Math.pow(potega,6)+Math.pow(potega,5)+Math.pow(potega,4)+Math.pow(potega,3)+Math.pow(potega,2)+Math.pow(potega,1);
		   var kredytbrutto = kredyt*(1+mnoznik);
			
			for(var i in PROGI){				
				if(raty >= PROGI[i][0] && raty <= PROGI[i][1]){
					opr = PROGI[i][2];
				}				
			}
			
			var r = PMT(opr/(12*100), raty,-1 * kredytbrutto );
			
			r = Math.round(r*100)/100;
			
			$('#rata').html(
				addSpaces(r) + ' zł'
			);
			
		};
		
		function PMT(i, n, p){
			return i * p * Math.pow((1+i), n) / (1 - Math.pow(1+i, n));			
		}
		
		function addSpaces(nStr)
		{
		  nStr += '';
		  x = nStr.split('.');
		  x1 = x[0];
		  x2 = x.length > 1 ? ',' + x[1] : '';
		  var rgx = /(\d+)(\d{3})/;
		  while (rgx.test(x1)) {
		    x1 = x1.replace(rgx, '$1' + ' ' + '$2');
		  }
		  return x1 + x2;
		}


	});

