
var first_payment = 0;
var currency = "";
var swtch = 0;

function chage_price(cena)
{

	price = parseFloat(cena);
	calculate_lease();
}

function math_round (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];
}

function convert_price(currency)
{
	if (currency == "LVL" && swtch == 0)
	{
		price = price*eur;
		swtch = 1;
	}
	else if (currency == "EUR" && swtch == 1)
	{
		price = price/eur;
		swtch = 0;
	}

	valuta = currency;		
	$("#first_payment_currency").html(currency);
	$("#lease-payment_currency").html(currency);
	$("#lease-payment_currency_total").html(currency);
	calculate_lease();
}

function show()
{
	if (price==0)
	{
		$("#c_price").val(0);
		$("#first_payment").html("0");
		$("#lease-payment").html("0");		
		$("#lease-payment_total").html("0");		
	}
	else
	{
		if (!isNaN(price) && price>0)
		{
			$("#c_price").val(math_round(price,0));
		}
		else
		{
			$("#c_price").val(0);	
		}
		
		if (!isNaN(first_payment) && first_payment>0)
		{
			$("#first_payment").html(math_round(first_payment,0));		
		}
		else
		{
			$("#first_payment").html("0");	
		}
		
		if (!isNaN(lease_payment) && lease_payment>0)
		{   
            //komijas maksa
            var mincom = ($("input[name=valuta]:checked").val() == "EUR")?(50/eur):50;
            var com = parseFloat(jQuery("#c_price").val());
                com = com * 0.015;
                com = com < mincom ? mincom:com;
                com = math_round(com * 1.22,0);
			$("#lease-payment").html(math_round(lease_payment,0))	
			$("#lease-payment_total").html(math_round(lease_payment,0) * parseInt($("#c_lease_period").val())+ parseFloat($("#first_payment").html()) + parseFloat(com));
		}
		else
		{
			$("#lease-payment").html("0");	
			$("#lease-payment_total").html("0");	
		}
	}
}
function calculate_lease()
{
	first_payment = price * $("#c_first_paymanet").val() / 100;


	if (valuta=="EUR") {percent = eur_rate + parseFloat($("#percen_rate").val());}
	if (valuta=="LVL") {percent = lvl_rate + parseFloat($("#percen_rate").val());}	

	lease_payment = ((percent / 100 / 12) * ((price - first_payment )-((price * ( lease_type == 1 ? residual_val : 0 )/*atlikusi vertiba*/ / 100) / (Math.pow((percent / 100 / 12) + 1, parseFloat($("#c_lease_period").val()))))) / (1 - (1 / Math.pow((percent / 100 / 12) + 1, parseFloat($("#c_lease_period").val())))));
	
	if ($("#kasko:checked").val())
	{
		lease_payment = lease_payment + calcKasko();	
	}
	
	show();
}

function calcKasko()
{
	if (kasko==0)
	{
		return 0;
	}
	else
	{
		var bta = new Array (
			new Array (4.54, 5.21, 6.00, 5.96, 6.12, 5.97, 6.14, 6.41, 6.90, 7.30, 7.93, 8.26, 8.61, 8.70, 9.09, 9.98, 11.74, 13.50), 		// 1. grupa
			new Array (4.77, 5.47, 6.30, 6.25, 6.42, 6.27, 6.45, 6.73, 7.25, 7.67, 8.32, 8.67, 9.04, 9.13, 9.54, 10.48, 12.05, 13.86), 		// 2. grupa
			new Array (3.96, 4.60, 4.97, 5.15, 5.34, 5.30, 5.57, 5.74, 5.83, 5.88, 5.97, 6.05, 6.14, 6.27, 6.43, 6.77, 7.79, 8.95), 		// 3. grupa
			new Array (5.49, 6.29, 7.25, 7.19, 7.38, 7.21, 7.42, 7.74, 8.34, 8.82, 9.57, 9.97, 10.40, 10.50, 10.97, 12.05, 13.86, 15.94), 	// 4. grupa
	
			new Array (5.49,6.29,7.25,7.19,7.38,7.21,7.42,7.74,8.34,8.82,9.57,9.97,10.40,10.50,10.97,12.05,13.86,15.94), 					// 5. grupa	
			new Array (3.63,4.17,4.80,4.77,4.90,4.78,4.91,5.13,5.52,5.84,6.34,6.61,6.89,6.96,7.27,7.98,9.39,10.80), 						// 6. grupa	
			new Array (3.86,4.43,5.10,5.07,5.20,5.07,5.22,5.45,5.87,6.21,6.74,7.02,7.32,7.40,7.73,8.48,9.98,11.48), 						// 7. grupa	
			new Array (4.09,4.69,5.40,5.36,5.51,5.37,5.53,5.77,6.21,6.57,7.14,7.43,7.75,7.83,8.18,8.98,10.57,12.15),	 					// 8. grupa	
			new Array (4.31,4.95,5.70,5.66,5.81,5.67,5.83,6.09,6.56,6.94,7.53,7.85,8.18,8.27,8.64,9.48,11.15,12.83),						// 9. grupa	
			new Array (4.77,5.47,6.30,6.26,6.43,6.27,6.45,6.73,7.25,7.67,8.33,8.67,9.04,9.14,9.54,10.48,12.33,14.18), 						// 10. grupa	
			new Array (4.99,5.73,6.60,6.56,6.73,6.57,6.75,7.05,7.59,8.03,8.72,9.09,9.47,9.57,10.00,10.98,12.91,14.85), 						// 11. grupa	
			new Array (5.22,5.99,6.90,6.85,7.04,6.87,7.06,7.37,7.94,8.40,9.12,9.50,9.90,10.01,10.45,11.48,13.50,15.53), 					// 12. grupa	
			new Array (5.45,6.25,7.20,7.15,7.34,7.16,7.37,7.69,8.28,8.76,9.52,9.91,10.33,10.44,10.91,11.98,14.09,16.20), 					// 13. grupa	
			new Array (3.82,4.38,5.04,5.00,5.14,5.02,5.16,5.38,5.80,6.14,6.66,6.94,7.23,7.30,7.63,8.38,9.64,11.09), 						// 14. grupa	
			new Array (4.05,4.65,5.36,5.31,5.46,5.33,5.48,5.72,6.16,6.52,7.07,7.37,7.68,7.76,8.11,8.91,10.24,11.78), 						// 15. grupa	
			new Array (4.29,4.92,5.67,5.63,5.78,5.64,5.81,6.06,6.53,6.90,7.49,7.80,8.14,8.22,8.59,9.43,10.85,12.47), 						// 16. grupa	
			new Array (4.53,5.20,5.99,5.94,6.10,5.96,6.13,6.39,6.89,7.29,7.90,8.24,8.59,8.67,9.06,9.96,11.45,13.17), 						// 17. grupa	
			new Array (5.01,5.74,6.62,6.56,6.74,6.58,6.77,7.07,7.61,8.05,8.74,9.10,9.49,9.59,10.02,11.00,12.65,14.55), 						// 18. grupa	
			new Array (5.25,6.02,6.93,6.88,7.06,6.90,7.10,7.40,7.98,8.44,9.15,9.54,9.94,10.04,10.49,11.53,13.26,15.25), 					// 19. grupa	
			new Array (5.49,6.29,7.25,7.19,7.38,7.21,7.42,7.74,8.34,8.82,9.57,9.97,10.40,10.50,10.97,12.05,13.86,15.94), 					// 20. grupa	
			new Array (5.72,6.56,7.56,7.50,7.70,7.52,7.74,8.08,8.70,9.20,9.98,10.40,10.85,10.96,11.45,12.58,14.46,16.63), 					// 21. grupa	
			new Array (3.17,3.68,3.98,4.12,4.27,4.24,4.46,4.59,4.66,4.70,4.78,4.84,4.91,5.02,5.14,5.42,6.23,7.16), 							// 22. grupa	
			new Array (3.37,3.91,4.22,4.38,4.54,4.51,4.73,4.88,4.96,5.00,5.07,5.14,5.22,5.33,5.47,5.75,6.62,7.61), 							// 23. grupa	
			new Array (3.56,4.14,4.47,4.64,4.81,4.77,5.01,5.17,5.25,5.29,5.37,5.45,5.53,5.64,5.79,6.09,7.01,8.06), 							// 24. grupa	
			new Array (3.76,4.37,4.72,4.89,5.07,5.04,5.29,5.45,5.54,5.59,5.67,5.75,5.83,5.96,6.11,6.43,7.40,8.50), 							// 25. grupa	
			new Array (4.16,4.83,5.22,5.41,5.61,5.57,5.85,6.03,6.12,6.17,6.27,6.35,6.45,6.58,6.75,7.11,8.18,9.40), 							// 26. grupa	
			new Array (4.36,5.06,5.47,5.67,5.87,5.83,6.13,6.31,6.41,6.47,6.57,6.66,6.75,6.90,7.07,7.45,8.57,9.85), 							// 27. grupa	
			new Array (4.55,5.29,5.72,5.92,6.14,6.10,6.41,6.60,6.70,6.76,6.87,6.96,7.06,7.21,7.39,7.79,8.96,10.29),	 						// 28. grupa	
			new Array (4.75,5.52,5.96,6.18,6.41,6.36,6.68,6.89,7.00,7.06,7.16,7.26,7.37,7.52,7.72,8.12,9.35,10.74),	 						// 29. grupa	
			new Array (4.39,5.03,5.80,5.75,5.91,5.77,5.93,6.19,6.67,7.06,7.65,7.98,8.32,8.40,8.78,9.64,11.09,12.75),	 					// 30. grupa	
			new Array (4.66,5.35,6.16,6.11,6.28,6.13,6.30,6.58,7.09,7.50,8.13,8.47,8.84,8.92,9.33,10.24,11.78,13.55), 						// 31. grupa	
			new Array (4.94,5.66,6.52,6.47,6.64,6.49,6.68,6.97,7.50,7.94,8.61,8.97,9.36,9.45,9.87,10.85,12.47,14.35), 						// 32. grupa	
			new Array (5.21,5.98,6.88,6.83,7.01,6.85,7.05,7.35,7.92,8.38,9.09,9.47,9.88,9.97,10.42,11.45,13.16,15.14), 						// 33. grupa	
			new Array (5.76,6.61,7.61,7.55,7.75,7.57,7.79,8.13,8.75,9.26,10.05,10.47,10.92,11.02,11.52,12.65,14.55,16.74),				 	// 34. grupa	
			new Array (6.03,6.92,7.97,7.91,8.12,7.93,8.16,8.51,9.17,9.70,10.52,10.97,11.44,11.55,12.07,13.26,15.24,17.53), 					// 35. grupa	
			new Array (6.31,7.23,8.33,8.27,8.49,8.29,8.53,8.90,9.59,10.14,11.00,11.47,11.96,12.07,12.62,13.86,15.94,18.33), 				// 36. grupa	
			new Array (6.58,7.55,8.69,8.63,8.86,8.65,8.90,9.29,10.01,10.58,11.48,11.96,12.48,12.60,13.17,14.46,16.63,19.13), 				// 37. grupa	
			new Array (4.39,5.03,5.80,5.75,5.91,5.77,5.93,6.19,6.67,7.06,7.65,7.98,8.32,8.40,8.78,9.64,11.09,12.75),	 					// 38. grupa	
			new Array (4.66,5.35,6.16,6.11,6.28,6.13,6.30,6.58,7.09,7.50,8.13,8.47,8.84,8.92,9.33,10.24,11.78,13.55), 						// 39. grupa	
			new Array (4.94,5.66,6.52,6.47,6.64,6.49,6.68,6.97,7.50,7.94,8.61,8.97,9.36,9.45,9.87,10.85,12.47,14.35),						// 40. grupa	
			new Array (5.21,5.98,6.88,6.83,7.01,6.85,7.05,7.35,7.92,8.38,9.09,9.47,9.88,9.97,10.42,11.45,13.16,15.14), 						// 41. grupa	
			new Array (5.76,6.61,7.61,7.55,7.75,7.57,7.79,8.13,8.75,9.26,10.05,10.47,10.92,11.02,11.52,12.65,14.55,16.74),					// 42. grupa	
			new Array (6.03,6.92,7.97,7.91,8.12,7.93,8.16,8.51,9.17,9.70,10.52,10.97,11.44,11.55,12.07,13.26,15.24,17.53), 					// 43. grupa	
			new Array (6.31,7.23,8.33,8.27,8.49,8.29,8.53,8.90,9.59,10.14,11.00,11.47,11.96,12.07,12.62,13.86,15.94,18.33), 				// 44. grupa	
			new Array (6.58,7.55,8.69,8.63,8.86,8.65,8.90,9.29,10.01,10.58,11.48,11.96,12.48,12.60,13.17,14.46,16.63,19.13) 				// 45. grupa		
		);
		
		
		var date = new Date();
        var cyear = date.getFullYear();
		var vecums = cyear-year;
		
		if (valuta=="LVL")
		{
			var ins_price = price;	
		}
		else if (valuta=="EUR")
		{
			var ins_price = price * eur;	
		}	
		
		coef = bta[kasko-1][vecums];
		insprice=ins_price * coef / 100;

		if (ins_price > 12000 && ins_price <= 15000)
		{
			discount = 0.05;	
		} else if (ins_price > 15000 && ins_price <= 20000)
			{
				discount = 0.12;	
			} else if (ins_price > 20000 && ins_price <= 40000)
				{
					discount = 0.2;	
				} else if (ins_price > 40000)
					{
						discount = 0.25;	
					}
					else
					{
						discount = 0;
					}
		
		if(discount) {insprice = insprice - insprice * discount;}
		if (insprice < 200) {insprice = 200;}
		
		insprice = insprice/12;
		
		if (valuta=="LVL")
		{
			insprice = insprice;	
		}
		else if (valuta=="EUR")
		{
			insprice = insprice / eur;	
		}			

		insprice = math_round(insprice,0);

		return parseFloat(insprice);
	}
}

function changeType()
{
	lease_type = $("#c_lease_type").val();
	
	if (lease_type==2)
	{
		$("#residual").hide();
	}
	else if (lease_type==1)
	{
		$("#residual").show();
	}
	
	changePeriod();
}

function changePeriod()
{
	select_val = $("#c_lease_period").val();
	$("#c_lease_period").html("");

	if (lease_type==2)
	{
		for( var i = 84; i >= 12; i = i-12 )
		{
			var option = document.createElement('option');
			option.value = i;
			option.innerHTML = i+" "+monthes;
			if( i == select_val ){ option.selected = true; }
			$("#c_lease_period").append(option);
		}
			var option = document.createElement('option');
			option.value = 6;
			option.innerHTML = 6+" "+monthes;
			if( 6 == select_val ){ option.selected = true; }
			$("#c_lease_period").append(option);		
	}
	else if (lease_type==1)
	{
		for( var i = 60; i >= 12; i = i-12 )
		{
			var option = document.createElement('option');
			option.value = i;
			option.innerHTML = i+" "+monthes;
			if( i == select_val ){ option.selected = true; }
			$("#c_lease_period").append(option);
		}
			var option = document.createElement('option');
			option.value = 6;
			option.innerHTML = 6+" "+monthes;
			if( 6 == select_val ){ option.selected = true; }
			$("#c_lease_period").append(option);		
	}
	residual_value()
	calculate_lease();
}

function changeMoths()
{
	residual_value();
	calculate_lease();
}

function residual_value()
{
	if (lease_type==2)
	{
		residual_val = 0;
	}
	else
	{
		if ($("#c_lease_period").val()==60) {residual_val = 30;}
		if ($("#c_lease_period").val()==48) {residual_val = 40;}
		if ($("#c_lease_period").val()==36) {residual_val = 50;}
		if ($("#c_lease_period").val()==24) {residual_val = 60;}
		if ($("#c_lease_period").val()==12) {residual_val = 70;}
		if ($("#c_lease_period").val()==6)	 {residual_val = 70;}

		$("#residual_value").html(residual_val+'%');
	}
}


