/**
Title: Credit Calculator for Unicreditbank
Company: Internet Solutions Agency Mediaparks
Author: Mārcis Pauls (info@mediaparks.lv)
Last updated: 07.08.2007
*/
var $calc = new calculator();
$(document).ready(
	function ()	{
		$calc.init();
		$(".calculator").submit(
			function ()
			{
				return false;
			}
		);
		$("#calc_type").change(
			function () {
				$calc.type = $(this).val();
				$calc.init();
			}
		);
	}
);

function calculator()
{
	this.type			= 1;
	this.typeToHide 	= 2;
	this.creditSum		= 0;	// kredīta summa
	this.parcent		= 6;	// procenti
	this.term			= 12;	// termiņš
	this.paymentType	= 0;	// atmaksas tips: 0 - fiksēts, 1 - dilstošs
	this.monthlyPayment	= 0;	// mēneša maksājums
	this.parcent2		= this.parcent/100;
		
	this.init = function () {
		//	reset to default values
		this.clearValues();
				
		if(this.type == 2) {
			this.typeToHide = 1;
		} else {
			this.typeToHide = 2;
		}
		$(".type"+this.type).show();
		$(".type"+this.typeToHide).hide();
		
		$("#calculate1").click(
			function () {
				$calc.calculateMonthlyPayment();
			}
		);
		$("input[@name=payment_type]").click(
			function () {
				$calc.calculateMonthlyPayment();
			}
		);
		
		$("#calculate2").click(
			function () {
				$calc.calculateCreditSum();
			}
		);
		
		$("#show_graphic").popupwindow();
	};
	
	this.calculateMonthlyPayment = function () {
		this.setCreditSum();
		this.setParcent();
		this.setTerm();
		this.setPaymentType();
		$(".payment2").show();
		
		if(this.paymentType == 0) {
			this.monthlyPayment = (this.creditSum*this.parcent2/12)/(1-Math.pow((1+this.parcent2/12), -this.term));
			this.monthlyPayment = 100*this.monthlyPayment;
			this.monthlyPayment = Math.ceil(this.monthlyPayment)/100;
			this.monthlyPayment = parseFloat(this.monthlyPayment);
		} else {
			this.monthlyPayment = "";
			$(".payment2").hide();
		}
		$("#calc_month").val(this.monthlyPayment);
	};
	
	this.setCreditSum = function () {
		this.creditSum = $("#credit_sum").val() || "0";
		this.creditSum = this.creditSum.replace(",", ".");
		this.creditSum = parseFloat(this.creditSum);
		if(isNaN(this.creditSum)) {
			this.creditSum = 0;
		}
		$("#credit_sum").val(this.creditSum);
	};
	
	this.setParcent = function () {
		this.parcent = $("#credit_parcent").val().replace(',','.') || 6;
		this.parcent = parseFloat(this.parcent);
		if(isNaN(this.parcent)) {
			this.parcent = 6;
		}
		this.parcent2 = this.parcent/100;
		$("#credit_parcent").val(this.parcent);
	};
	
	this.setTerm = function () {
		this.term = $("#credit_length").val() || 12;
		this.term = parseInt(this.term);
		if(isNaN(this.term)) {
			this.term = 12;
		}
		$("#credit_length").val(this.term);
	};
	
	this.setPaymentType = function () {
		this.paymentType = ($("#payment_type1").attr("checked") ? 0 : 1);
	};
	
	this.setPaymentType2 = function () {
		this.paymentType = ($("#payment_type3").attr("checked") ? 0 : 1);
	};
	
	this.calculateCreditSum = function () {
		this.setMonthlyPayment();
		this.setParcent();
		this.setTerm();
		this.setPaymentType2();
		
		if(this.paymentType == 0) {
			this.creditSum = (this.monthlyPayment*(1-Math.pow((1+this.parcent2/12),-this.term)))/(this.parcent2/12);
		} else {
			this.creditSum = (this.monthlyPayment*12*this.term)/(12+this.parcent2*this.term);
		}
		this.creditSum = 100*this.creditSum;
		this.creditSum = Math.ceil(this.creditSum)/100;
		this.creditSum = parseFloat(this.creditSum);
		$("#calc_credit").val(this.creditSum);
	};
	
	this.setMonthlyPayment = function () {
		this.monthlyPayment = $("#monthly_payment").val() || "0";
		this.monthlyPayment = this.monthlyPayment.replace(",", ".");
		this.monthlyPayment = parseFloat(this.monthlyPayment);
		if(isNaN(this.monthlyPayment)) {
			this.monthlyPayment = 0;
		}
		$("#monthly_payment").val(this.monthlyPayment);
	}
	
	this.clearValues = function () {
		$("#credit_sum").val("");
		$("#credit_parcent").val(this.parcent);
		$("#credit_length").val(this.term);
		$("#payment_type1").attr("checked", "checked");
		$("#payment_type3").attr("checked", "checked");
		$("#calc_month").attr("");
		$("#monthly_payment").val("");
	};
}
