var creditCalc={config:{percentage:6.9,period:30},data:{sumCreditCost:"",sumInterest:"",instalment:[]},resultBox:false,init:function(){$("creditCalc").observe("submit",function(a){a.stop();this.validate()}.bind(this));this.calculate();if(this.getPrice()){this.generateTable()}},validate:function(){if($("calcError")){$("calcError").remove()}if(this.resultBox){this.resultBox.update()}this.error=new Array();if($("price").value==""){this.error.push(this.tr.errorPriceEmpty)}else{if(isNaN(parseFloat($("price").value.replace(",",".")))){this.error.push(this.tr.errorPriceNumber)}}if($("period").value==""){this.error.push(this.tr.errorPeriodEmpty)}else{if(isNaN(parseInt($("period").value))){this.error.push(this.tr.errorPeriodNumber)}}if($("percentage").value==""){this.error.push(this.tr.errorPercentageEmpty)}else{if(isNaN(parseFloat($("percentage").value))){this.error.push(this.tr.errorPercentageNumber)}}if(this.error.length){var c="";for(var b=0,a=this.error.length;b<a;b++){c+="<li>"+this.error[b]+"</li>"}var d=new Element("div",{id:"calcError","class":"errorBox"}).update("<ul>"+c+"</ul>");$("contentOD").insertBefore(d,$("creditCalc"))}else{this.reCalculate()}},calculate:function(a){this.data.instalment.length=0;this.setPrice();this.setPercentage();this.setPeriod();if($("instalment").value==="equalInstalment"){this.calculateEqual()}else{this.calculateDeclining()}},reCalculate:function(){this.calculate();this.generateTable()},getPrice:function(){return $("price").value.replace(",",".")},setPrice:function(){if($("price").value){$("price").value=this.numberFormat(parseFloat($("price").value.replace(",",".")))}},getPercentage:function(){return $("percentage").value.replace(",",".")},setPercentage:function(){if($("percentage").value){$("percentage").value=this.numberFormat(parseFloat($("percentage").value.replace(",",".")))}else{$("percentage").value=this.config.percentage}},getPeriod:function(){return $("period").value},setPeriod:function(){if($("period").value){$("period").value=parseInt($("period").value)}else{$("period").value=this.config.period}},calculateEqual:function(){if($("periodType").value==="yearPeriod"){var d=this.getPeriod()}else{var d=this.getPeriod()/12}var h=this.getPercentage();var b=this.getPrice();var e=12;var f=d*e;var j=(1-1/Math.pow(1+((h/100)/e),f))/((h/100)/e);this.data.sumCreditCost=this.numberFormat((b/j)*f);this.data.sumInterest=this.numberFormat(((b/j)*f)-b);var a=b/j;for(i=1;i<=f;i++){var c=(1/(Math.pow(1+((h/100)/e),(f-i)+1)))*a;var g=a-c;this.data.instalment[i-1]=[a,g,c]}},calculateDeclining:function(){if($("periodType").value==="yearPeriod"){var e=this.getPeriod()}else{var e=this.getPeriod()/12}var j=this.getPercentage();var c=this.getPrice();var f=12;var g=e*f;var d=c/g;var b=0;for(i=1;i<=g;i++){var h=((j/100)/f)*(c-(d*(i-1)));b+=h;var a=d+h;this.data.instalment[i-1]=[a,h,d]}this.data.sumCreditCost=this.numberFormat(Number(c)+b);this.data.sumInterest=this.numberFormat(b)},numberFormat:function(a){return a.toFixed(2).replace(".",",")},generateTable:function(){var a="<fieldset><h4>"+this.tr.txtInfo+'</h4><div><p class="fourP">'+this.tr.txtChargeForCredit+": <strong>"+this.data.sumCreditCost+'</strong></p><p class="fourP">'+this.tr.txtInterest+": <strong>"+this.data.sumInterest+'</strong></p></div></fieldset><fieldset><h4 class="noBorder">'+this.tr.txtRepaymentPlan+'</h4><div id="calcList"></div><p class="fourP">'+this.tr.txtInfoApproximateOnly+"</p></fieldset>";if(!this.resultBox){this.resultBox=new Element("div",{id:"calcResult","class":"formBoxOD"});this.resultBox.observe("click",function(b){b.stop();if(b.target.nodeName==="A"){this.generateList(parseInt(b.target.rel))}}.bind(this));$("contentOD").appendChild(this.resultBox)}this.resultBox.update(a);this.generateList()},generateList:function(g){var a=1;if(Object.isNumber(g)&&g>0){a=g}var e=this.data.instalment.length;var d='<div class="pageSplitter"><div class="pageCurrent">';if(a>2){d+='<a href="#" rel="0">«</a>\n'}if(a>1){d+='<a href="#" rel="'+(a-1)+'">‹</a>\n'}var c=a-4;while(c<1){c++}var b=c+10;while(b>Math.ceil(e/12)+1){b--;if(c-1>0){c--}}for(c,b;c<b;c++){if(c===a){d+="<strong>"+c+"</strong>\n"}else{d+='<a rel="'+c+'" href="#">'+c+"</a>\n"}}if(a<e/12){d+='<a href="#" rel="'+(a+1)+'">›</a>\n'}if(a<e/12-1){d+='<a href="#" rel="'+Math.ceil(e/12)+'">»</a>\n'}d+='</div><!-- /pageCurrent --><div class="pageNumber">'+this.tr.txtInstalmentNumber+": <span>"+e+'</span></div></div><table class="insertListOD"><tr><th>'+this.tr.txtNO+"</th><th>"+this.tr.txtInstalment+"</th><th>"+this.tr.txtInterest+"</th><th>"+this.tr.txtCapital+"</th></tr>";var h=0;var k=0;var f=0;var c=(a-1)*12;var b=c+12>e?e:c+12;for(c,b;c<b;c++){if(this.data.instalment[c]!=undefined){d+='<tr class="'+(c%2?"dark":"light")+'Blue"'+(c>0?' style="ddisplay:none"':"")+"><td>"+(c+1)+"</td><td>"+this.numberFormat(this.data.instalment[c][0])+"</td><td>"+this.numberFormat(this.data.instalment[c][1])+"</td><td>"+this.numberFormat(this.data.instalment[c][2])+"</td></tr>";h+=this.data.instalment[c][0];k+=this.data.instalment[c][1];f+=this.data.instalment[c][2]}}d+="<tr><th>"+this.tr.txtTotal+"</th><th>"+this.numberFormat(h)+"</th><th>"+this.numberFormat(k)+"</th><th>"+this.numberFormat(f)+"</th></tr></table>";$("calcList").update(d)}};document.observe("dom:loaded",function(){creditCalc.init()});
