function add_to_favorites()
{
	$url = window.location.href;
	$title = window.document.title;
	if (window.sidebar) {
		window.sidebar.addPanel($title, $url, "");
	} else if(window.external) {
		window.external.AddFavorite($url, $title);
	}
	return false;
}

function save_as()
{
	return false;
}

function print_it($href)
{
	var $parameters = "height=" + 600 + ",width=" + 800 + ",toolbar=" + 0 + ",scrollbars=" + 1  + ",status=" + 0 + ",resizable=" + 0 + ",left=" + (screen.width-800)/2;  + ",screenX=" + (screen.width-800)/2; + ",top=" + (screen.width-800)/2;  + ",screenY=" + (screen.width-800)/2;
	window.open($href, 'print_it', $parameters).focus();
	return false;
}

$(document).ready(
	function () {
		$("#leasing-type").change(
			function () {
				if(this.value == 0) {
					$("#leasing-type-1").show();
				} else {
					$("#leasing-type-1").hide();
				}
				calculate();
			}
		)
		$("#cost").change(
			function () {
				$("#firstCost2").val( Math.round($("#firstCost").val()*this.value)/100);
				$("#leftCost2").val(Math.round($("#leftCost").val()*$("#cost").val())/100);
				calculate();
			}
		)
		$("#firstCost").change(
			function () {
				$("#firstCost2").val(Math.round(this.value*$("#cost").val())/100);
				calculate();
			}
		)
		$("#firstCost2").change(
			function () {
				$("#firstCost").val(Math.round(this.value/$("#cost").val()*100));
				calculate();
			}
		)
		$("#leasing-period").change(
			calculate
		);
		$("#leasing-parcent").change(
			calculate
		);
		$("#leftCost").change(
			function () {
				$("#leftCost2").val(Math.round(this.value*$("#cost").val())/100);
				calculate();
			}
		);
		$("#leftCost2").change(
			function () {
				$("#leftCost").val(Math.round(this.value/$("#cost").val()*100));
				calculate();
			}
		);
		
	}
)

function calculate()
{
	$cost		= $("#cost").val(); // cena
	$firstCost	= $("#firstCost").val(); //pirm�? iemaksa %
	$firstCost2	= $("#firstCost2").val(); //pirm�? iemaksa
	$type		= $("#leasing-type").val(); // tips 0 - operatīvais, 1 - finan�?u
	$leasingPeriod = $("#leasing-period").val(); // ilgums mēne�?os
	$leasingParcent = $("#leasing-parcent").val(); // procentu likme
	$leftCost	= $("#leftCost").val(); // atlikusī vērtība %
	$leasingParcent2 = $leasingParcent/100;
	if($type == 1) {
		$leftCost = 0;
	}
	$(".sideBarRedLine strong").html(Math.round(($leasingParcent2/12) * (($cost - $firstCost2)-($cost*$leftCost/100)/Math.pow($leasingParcent2/12+1, $leasingPeriod)) / (1 - 1/Math.pow($leasingParcent2/12+1, $leasingPeriod))));
	
}

function showMegaResult(valoda)
{
	if($("#leasing-type").val() == 1) {
		open("http://www.unicreditleasing.ee/calculator_fin.php?lang="+valoda+"&data="+$("#leasing-period").val()+":"+$("#leasing-parcent").val()+":"+$("#cost").val()+":"+$("#firstCost2").val()+":","","width=760,height=500,scrollbars=yes");
	} else {
		open("http://www.unicreditleasing.ee/calculator.php?lang="+valoda+"&type=&data="+$("#leasing-period").val()+":"+$("#leasing-parcent").val()+":"+$("#cost").val()+":"+$("#firstCost2").val()+":"+$("#leftCost2").val(),"","width=760,height=500,scrollbars=yes");
	}
}
