$(function(){
	


	var DnbCalc = function() {
		this.price = $('#dnbcalc_price');
		this.price_in_eur = $('#dnbcalc_price_in_eur');
		this.interest = $('#dnbcalc_interest');
		this.salvage_value = $('#dnbcalc_salvage_value');
		this.comission = $('#dnbcalc_comission');
		this.period = $('#dnbcalc_period');
		this.down_payment = $('#dnbcalc_down_payment');
		this.currency = $('#dnbcalc_currency');
		this.persontype = $('#dnbcalc_persontype');
		this.lease_type = $('#dnbcalc_lease_type');
		this.month_payment = $('#dnbcalc_month_payment');
		this.gpl = $('#dnbcalc_gpl');
		this.mode = $('#dnbcalc_mode');

		this.calculate = function() {		
			this.setSalvageValueSelectStatus();
			this.checkMode();
			this.setPrice();
			if (this.mode.val() == 1) {
				this.calcMode1();
			}
			else {
				this.calcMode0();
			}
			this.setDownPayment();
		}

		this.setDownPayment = function() {
			var price = parseInt(this.price.val());
			var down = parseInt(this.down_payment.val());
			var down_payment_sum = Math.round(price*down / 100);
			$('#dnbcalc_down_payment_sum').val(down_payment_sum);
			$('#dnbcalc_lease_total').html(price - down_payment_sum);
		}

		this.calcMode0 = function() {
			var nbCalcOut = this.calcPrepare();
			var nbCalcEx=$('#dnbcalc_currency option:selected').text();
			this.month_payment.html(nbCalcOut+"&nbsp;"+nbCalcEx);
			$('#dnbcalc_month_payment_label').html(gettext('Monthly payment'));
		}

		this.calcMode1 = function() {
			var nbCalcYear=this.period.val();
			var nbCalcTerm=Math.ceil(nbCalcYear)*12;
			var nbCalcOut = this.calcPrepare();
			var nbCalcVal=this.price.val();
			var nbCalcFm=this.down_payment.val();
			var nbCalcOm=nbCalcVal*(nbCalcFm / 100);
			
			var nbCalcComm=this.comission.val();
			var nbCalcCommSumm = (nbCalcVal) / 100 * nbCalcComm;
			
			var nbCalcEx=$('#dnbcalc_currency option:selected').text();

			this.month_payment.html(Math.round((nbCalcOut*nbCalcTerm + nbCalcCommSumm + nbCalcOm)*100)/100+"&nbsp;"+nbCalcEx);	
			$('#dnbcalc_month_payment_label').html(gettext('Total payment'));		
		}

		this.checkMode = function() {
			if (this.persontype.val() != 'f' || this.lease_type.val() != 'f') {
				this.mode.val(0);
				$('#dnbcalc_calculate_all_bt').addClass('hide');
			}
			else {
				$('#dnbcalc_calculate_all_bt').removeClass('hide');
			}
		}

		this.setMode = function(mode) {
			this.mode.val(mode);
		}

		this.setPrice = function() {
			if ($('#dnbcalc_currency').val()) {
				var symbol = $('#dnbcalc_currency option:selected').text();
				this.price.val(Math.round(this.price_in_eur.val() / $('#dnbcalc_currency').val()));
			}
		}

		this.calcPrepare = function() {
			var nbCalcOut=0;
			var nbCalcOutL=0;
			var nbCalcOOm=0;
			var nbCalcVal=this.price.val();
			var nbCalcFm=this.down_payment.val();
			if (!isNaN(nbCalcVal)&&nbCalcVal){
			
				var nbCalcOm=nbCalcVal*(nbCalcFm / 100);
				var nbCalcTp=this.lease_type.val();
				var nbCalcComm=this.comission.val();
				var nbCalcCommSumm = nbCalcVal / 100 * nbCalcComm;
				if (nbCalcTp=="o"){
					var nbCalcPm=this.salvage_value.val();
					var nbCalcOOm=nbCalcVal*(nbCalcPm / 100);
				} else var nbCalcOOm=0;
				var nbCalcPrc=this.interest.val();
				var nvCalcRate=nbCalcPrc/100/12;
				var nbCalcYear=this.period.val();
				var nbCalcTerm=Math.ceil(nbCalcYear)*12;
				
				nbCalcOutL=((nvCalcRate*((nbCalcVal-nbCalcOm)-(nbCalcOOm/(Math.pow(nvCalcRate+1,nbCalcTerm))))/(1-(1/Math.pow(nvCalcRate+1,nbCalcTerm)))));
				nbCalOut=Math.round(nbCalcOutL*100)/100;
			}
			
			nbCalcOut = Math.round(nbCalOut*100)/100;
			nbKlients = this.persontype.val();
			if((nbKlients=='f') && (nbCalcTp!="o")){
				$('#dnbcalc_gpl span').html(Math.round(this.getGPL(nbCalcVal-nbCalcOm-nbCalcCommSumm,nbCalcTerm,nbCalcOutL,nbCalcOOm)*100,2)/100 + '%');
				this.gpl.removeClass('hide');
			}
			else {
				this.gpl.addClass('hide');
			}
			
			return nbCalcOut;
		}

		this.setSalvageValueSelectStatus = function() {
			var disabled = true;
			if (this.lease_type.val() == 'o') {
				disabled = false;
			}
			this.salvage_value.attr('disabled', disabled);
		}

		this.getGPL = function(summ, termm, payment, pluss){

			i = 1; s = 0; payments=[];
			for(trm = 1; trm <= termm; trm++) {	s = s+payment;payments[trm] = payment;}
			s = s+pluss; payments[termm] = payments[termm] + pluss;

			if (s > summ) {
				x_res = x_res1 = x_res2 = x_ressum = x_ressum1 = x_ressum2 = 0; x_resnew = 1; x_diff = .1; xi = 0;
				while (true) {
					x_ressum = 0;
					for (trm = 1; trm <= termm; trm++) {
						x_ressum += payments[trm] / this.powX(1 + x_res, trm/12);
					}
					if(xi == 0){ x_ressum1 = x_ressum; x_res1 = x_res; x_res = x_res+x_diff;}
					else{
						if(xi == 1){ x_ressum2 = x_ressum; x_res2 = x_res; x_res = x_res1 + x_diff * (summ - x_ressum1) / (x_ressum2 - x_ressum1); }
						else{ x_resnew = x_res2 + (summ - x_ressum2) / ((x_ressum - x_ressum1) / (x_res - x_res1)); x_res1 = x_res2; x_ressum1 = x_ressum2; x_res2 = x_res; x_ressum2 = x_ressum; x_res = x_resnew;}
					}
					if (Math.round(x_resnew*1000000)/1000000 == Math.round(x_res2*1000000)/1000000 || xi > 10) { x_res = x_resnew; break;}
					xi = xi + 1;
				}
				result  = x_res * 100;
			}
			else result = 0;
			return result;
		}

		this.powX = function(zx, zc){
			if (zx == 0) {
				if (zc == 0) return false;
				else return 0;
			}
			if (zc == 0) return 1;
			if (Math.round(zc) == zc) {
				rx = 1;
				for (i = 0; i < zc; i++) rx = rx*zx;
				for (i = 0; i < -zc; i++) rx = rx/zx;
			}
			else if (zx < 0) return false;
			else {
				if (Math.abs(zc) > .5) return this.powX(zx, Math.round(zc)) * this.powX(zx, zc - Math.round(zc));
				rx = 1;px = 1; mx = Math.log(zx) * zc;
				for (i = 1; i < 100; i++) {
					px = px*mx/i; rx = rx+px;
					if (i > 100 || (Math.log((Math.abs(px)))/Math.log(10)) < -10) break;
				}
			}
			return rx;
		}
		return this;
	}
	
	$('#dnbcalc_calculate_bt').click(function() {
		DnbCalc().setMode(0);
		DnbCalc().calculate();
	});
	
	$('#dnbcalc_calculate_all_bt').click(function() {
		DnbCalc().setMode(1);
		DnbCalc().calculate();
	});
	$('#dnb-calc select').change(function() {
		DnbCalc().calculate();
	});
	$('#dnb-form #box input').keyup(function() {
		DnbCalc().calculate();
	});
	DnbCalc().calculate();

	$('#dnb-calc #link_to_form a').click(function() {
		$('#dnb-calc form').submit();
		return false;
	});
});