function c24_checkPurpose(hide) {
	var val=$('#input_vz').val();

	// Neu- und Gebrauchtfahrzeug
	if(val==1 || val==2){
		$('.rows_autokredit').show();
	}
	else{
		$('.rows_autokredit').hide();
	}
}



function c24_checkPurchasePrice() {
	if(jQuery("#input_az").length>0){
		var loanAmount = parseInt($("#input_kp").val().replace(/\./g,''));
	    var deposit = parseInt($("#input_az").val().replace(/\./g,''));
	    var purchasePrice = 0;
	    if (!isNaN(loanAmount)) {
	        if (isNaN(deposit)) {
	            deposit = 0;
	        }
	        purchasePrice = loanAmount + deposit;
	    }
	
	    $("#input_pp").val(c24_convertThousendPoint(purchasePrice));
	}
}

function c24_submitDuration(duration) {
	$('#input_lz').val(duration);
	$('form[name="calculator"]').submit();
	return false;
}

//display registration date
function c24_prepare(){
	c24_checkPurpose(true);
	c24_checkPurchasePrice();
	
	$(".c24-autosubmit").change(function() {
		if (c24.preventMultiSubmit(this.form))
		{
			this.form.submit();
		}
	});
	
	$('.toggle').click(function(){
		id=$(this).attr('id');
		$(this).toggleClass('close');
		if($(this).hasClass('close')){
			$(".t"+id).hide();
		}
		else{
			$(".t"+id).show();
		}
		return false;
	});	
	
	$('#input_vz').change(function() {
		c24_checkPurpose(false);
		c24_checkPurchasePrice();
	});
	
    $("#input_kp").keyup(function() {
	    c24_checkPurchasePrice();    	
    });
    $("#input_pp,#input_az").keyup(function() {
	    var purchasePrice = parseInt($("#input_pp").val().replace(/\./g,''));
	    var deposit = parseInt($("#input_az").val().replace(/\./g,''));
	    var loanAmount = 0;
	    
	    if (!isNaN(purchasePrice)) {
	        if (isNaN(deposit)) {
	            deposit = 0;
	        }
	        loanAmount = purchasePrice - deposit;
	    }
	
	    $("#input_kp").val(c24_convertThousendPoint(loanAmount));
	});
	$('.c24-month-selector-list li').each(function(){
		var id = $(this).attr('id').split('_');
		if (id[2] != undefined) {
			duration = id [2];
			
			$(this).click(function(){
				id = $(this).attr('id').split('_');
				return c24_submitDuration(id [2]);
			});
		}
	});
}

selectBoxes = {
	
	selectBox: 		'.selectBox',
	selectBoxList: 	'.selectionList',
	
	init: function(){
		
		$(selectBoxes.selectBox).click(function(){			
			selectBoxes.toggleList(this);
		});
		
		$(selectBoxes.selectBoxList+" li").click(function(){
			
			var obj = $(this).parent().siblings(selectBoxes.selectBox);
			
			var params = {	'obj':		obj,
							'value':	$(this).attr("value"), 
							'text':		$(this).text()
						};

			selectBoxes.setValue(params);
			selectBoxes.toggleList(obj);
		});
		$(selectBoxes.selectBoxList).mouseleave(function(){
			selectBoxes.toggleList();
		});
		
		selectBoxes.setDefaultValue();
	},
	
	setDefaultValue: function(){		

		$(selectBoxes.selectBox).each(function(){		
			
			var obj = $(this).siblings("select");
			var params = {	'obj':		obj,
							'value': 	obj.val(),
							'text': 	$("option:selected",obj).text()
							};
			
			var selectGfx = obj.parent().find(selectBoxes.selectBox);		
			selectGfx.text(params.text);
			
			selectBoxes.showCarOptions(params);
			//selectBoxes.setValue(params);
		});
	},
	
	setValue: function(params){	
		
		// Set 'value' of gfx select boxes
		
		var obj = params.obj;
		
		var selectBox = obj.parent().find('select');
		var selectGfx = obj.parent().find(selectBoxes.selectBox);
		
		selectBox.val(params.value);
		selectGfx.text(params.text);
		
		obj.parents("form").submit();
	},
	
	getValue: function(selectBox){
		
		$(selectBox).val();	
	},
	
	showCarOptions: function(params){
		
		if((params.value == "1") || (params.value == "2")){	
			$(".c24-calculator-interest-view").css("height","auto");
			$(".c24-car").show();
			
			var calcHeight = $(".c24-calculator-interest-view").height();
			$(".c24-calculator-interest-view").css("height",calcHeight);	
		}		
	},
	
	toggleList: function(clickedBox){		
		
		$(selectBoxes.selectBoxList).not($(clickedBox).siblings(selectBoxes.selectBoxList)).slideUp();
		
		$(clickedBox).siblings(selectBoxes.selectBoxList).slideToggle("fast");
		
	}
}

$(document).ready(function() {
	c24_prepare();
	
	selectBoxes.init();
    
});


