var open_tip=null;
var results_to_show=10;


function refreshVz(){
	var vz=$('#select_vz').val();
	if(vz==1|| vz==2 ){
		$('#row_car_fields').show();
		
		$('#car_price').val($('#credit_loan').val());
		$('#credit_loan').attr('disabled',true);
		$('#car_price').attr('disabled',false);
		
	}
	else{
		$('#row_car_fields').hide();
		$('#credit_loan').attr('disabled',false);
		$('#car_price').attr('disabled',true);
	}
}

function refreshCarPrice(){
	var vz=$('#select_vz').val();
	if(vz==1|| vz==2 ){
		 var purchasePrice = parseInt($("#car_price").val().replace(".","").replace(",","."));
	     var deposit = parseInt($("#car_debit").val().replace(".","").replace(",","."));
	     var loanAmount = 0;
	     
	     if (!isNaN(purchasePrice)) {
	         if (isNaN(deposit)) {
	             deposit = 0;
	         }
	         loanAmount = purchasePrice - deposit;
	     }
	 
	     $("#credit_loan").val(loanAmount);
	}
}

function submit_lz(lz){
	$('#cs_lz').val(lz);
	$('#calculator_details_form').submit();
}


$(function(){
    $("#car_price,#car_debit").keyup(function() {refreshCarPrice();});
    
	$('.linkallresults').click(function(){
		for(i=results_to_show+1;i<20;i++){
				$('.r'+i).show();	
			}
			
			$('.linkallresults').hide();
			$('.linknotallresults').show();
		});
	
	$('#select_vz').change(function(){refreshVz();});

	$('.toggle').click(function(){
		id=$(this).attr('id');
		$(".t"+id).toggle();
		$(this).toggleClass('close');
		return false;
	});
	
	$('#calculator_details').click(function(){
		$(this).toggleClass('close');
		$('#calculator_details_form_div').toggle();
	});
	
	
	$('.linknotallresults').click(function(){
		for(i=results_to_show+1;i<20;i++){
			$('.r'+i).hide();	
		}
		$('.linkallresults').show();
		$('.linknotallresults').hide();
	});
	
	$('.tip').click(function(){
		return false;
	});

	
	$('.tip_calc_button').click(function(ev){
		ev.stopPropagation();
		return true;
	});
	
	
	$('.infolink, .closetip').click(function(){
		if(open_tip!=null){
			lastId=open_tip.attr('id');
			open_tip.hide();
			open_tip=null;
			
			if(lastId!="tip_"+$(this).attr('id')){
				tip=$('#tip_'+$(this).attr('id'));
				tip.show();
				open_tip=tip;
			}
			
		} else {
			tip=$('#tip_'+$(this).attr('id'));
			tip.show();
			open_tip=tip;
		}
		return false;
	});

	$('#duration_12').click(function(){submit_lz(12);});
	$('#duration_24').click(function(){submit_lz(24);});
	$('#duration_36').click(function(){submit_lz(36);});
	$('#duration_48').click(function(){submit_lz(48);});
	$('#duration_60').click(function(){submit_lz(60);});
	$('#duration_72').click(function(){submit_lz(72);});
	$('#duration_84').click(function(){submit_lz(84);});
	$('#duration_96').click(function(){submit_lz(96);});
	$('#duration_108').click(function(){submit_lz(108);});
	$('#duration_120').click(function(){submit_lz(120);});
	
	
	$.each($(".input-currency"), function(key, input) {
			$(input).numeric({allow:".,"});
			c24_insertThousendPoint(input);
			$(input).focus(function(e) {
				c24_removeThousendPoint(e);
				this.select();
			});
			$(input).blur(c24_updateThousendPoint);
			$(input).change(c24_updateThousendPoint);
	});		
	
	
	$(document).click(function(){
		if(open_tip!=null){
			open_tip.hide();
			open_tip=null;
		}
	});
	
	refreshVz();
	refreshCarPrice();
});
