//kalkulator motor
/*
function credigenKalkulatorObject(name, penznem, kamatlab, halasztas, futamido_min, futamido_max, hitelosszeg_min, hitelosszeg_max, torleszto_min, torleszto_max, kezelesiktsg_type, kezelesiktsg_value, biztositas, onero_min, onero_max, default_futamido, default_onero, text1, urltext, url, thm_min, thm_max){

    this.name = name;
    this.penznem = penznem;
    this.kamatlab = kamatlab;
    this.halasztas = halasztas;
    this.futamido_min = futamido_min;
    this.futamido_max = futamido_max;
    this.hitelosszeg_min = hitelosszeg_min;
    this.hitelosszeg_max = hitelosszeg_max;
    this.torleszto_min = torleszto_min;
    this.torleszto_max = torleszto_max;
    this.kezelesiktsg_type = kezelesiktsg_type;
    this.kezelesiktsg_value = kezelesiktsg_value;
    this.biztositas = biztositas;
    this.onero_min = onero_min;
    this.onero_max = onero_max;
    this.default_futamido = default_futamido;
    this.default_onero = default_onero;
    this.thm_min = thm_min;
    this.thm_max = thm_max;

    this.ar = 0;
    this.hitelosszeg = 0;
    this.onero = 0;
    this.futamido = 0;
    this.irvaluea = 0;
    this.irvalueb = 0;
    this.havibiztositas = 0;
    this.torlesztoalap = 0;
    this.torleszto = 0;
    this.torlesztoelso = 0;
    this.torlesztobiztositassalelso = 0;
    this.torlesztopostponed = 0;
    this.thmmensu = 0;
    this.thm = 0;
    this.postpone = 0;
    this.postponeinterest = 0;
    this.kezelesikoltseghavi = 0;
    this.kezelesikoltsega = 0;
    this.kezelesikoltsegb = 0;
    
    this.ampRemainingCapital = new Array();
    this.ampCapital = new Array();
    this.ampInstalments = new Array();
    this.ampInterest = new Array();
    this.ampHandlingFee = new Array();

    this.ampRemainingCapitalBEA = new Array();
    this.ampInstalmentBEA = new Array();
    this.ampInterestBEA = new Array();
    this.ampHandlingFeeBEA = new Array();
    this.ampCapitalBEA = new Array();

    this.MAX_IRR_ITERATIONS=32000;

    this.boxWidth=150;
    this.error='';

    //THM-et szamol
    this.calculateTHM = function(){
        this.ampRemainingCapital = new Array();
        this.ampCapital = new Array();
        for(i=0;i<=this.futamido_max;i++){
            this.ampRemainingCapital[i]=0;
            this.ampCapital[i]=0;
            this.ampInstalments[i]=0;
            this.ampInterest[i]=0;
            this.ampHandlingFee[i]=0;
            
            this.ampRemainingCapitalBEA[i]=0;
            this.ampInstalmentBEA[i]=0;
            this.ampInterestBEA[i]=0;
            this.ampHandlingFeeBEA[i]=0;
            this.ampCapitalBEA[i]=0;
        }
		
//BEA --------------------------------------------------------------------------
        for(i=1;i<=this.futamido;i++){
	
			//ampRemainingCapitalBEA
        	if(i==1)this.ampRemainingCapitalBEA[i]=this.hitelosszeg;
        	else this.ampRemainingCapitalBEA[i]=this.ampRemainingCapitalBEA[i-1]-this.ampCapitalBEA[i-1];

			//ampInstalmentBEA
        	if(i==1)this.ampInstalmentBEA[i]=this.torlesztoelso;
        	else {
        		if(i==this.futamido){
        			this.ampInstalmentBEA[i]=this.torlesztoelso; 				// +H16
        		} else {
        			this.ampInstalmentBEA[i]=this.torlesztoelso;
        		}
			}
			
			//ampInterestBEA
			if(i==1)this.ampInterestBEA[i]=(this.ampRemainingCapitalBEA[i]+this.postponeinterest)*this.irvaluea+this.postponeinterest;
			else this.ampInterestBEA[i]=this.ampRemainingCapitalBEA[i]*this.irvaluea;
			
			//ampHandlingFeeBEA
			if(i==1){
				if(this.kezelesiktsg_type==3){
					this.ampHandlingFeeBEA[i]=this.kezelesikoltseghavi;
				} else if(this.kezelesiktsg_type==2){
					this.ampHandlingFeeBEA[i]=this.kezelesikoltsega;
				} else {
					this.ampHandlingFeeBEA[i]=this.kezelesikoltsega;			// SHOP
				}
			} else {
				if(this.kezelesiktsg_type==3) this.ampHandlingFeeBEA[i]=this.kezelesikoltseghavi;
			}

			//ampCapitalBEA
			if(i==this.futamido)this.ampCapitalBEA[i]=this.ampRemainingCapitalBEA[i];
			else this.ampCapitalBEA[i]=this.ampInstalmentBEA[i]-this.ampInterestBEA[i]-this.ampHandlingFeeBEA[i];
		}
		
		//init
        this.ampRemainingCapital[0]=this.hitelosszeg;

        if(this.kezelesiktsg_type==1){
	        this.ampInstalments[0]=-(this.hitelosszeg-this.kezelesikoltsega);
	    } else {
	        this.ampInstalments[0]=-this.hitelosszeg;
	    }
        
        for(i=1;i<=this.futamido+this.postpone;i++){
        //ampRemainingCapital
            this.ampRemainingCapital[i]=this.ampRemainingCapital[i-1]-this.ampCapital[i-1];
            
        //ampInstalments
            if(i>this.futamido+this.postpone){
                //
            } else if(i>this.postpone+1){
                this.ampInstalments[i]=this.torleszto;
            } else if(i==this.postpone+1){
                this.ampInstalments[i]=this.torlesztoelso;
            }

        //ampInterest
            if(i<this.postpone+1){
                //
            } else if(i==this.postpone+1){
                this.ampInterest[i]=Math.round(((this.ampRemainingCapital[i]+this.postponeinterest)*this.irvaluea+this.postponeinterest)*100000000000)/100000000000;
            } else {
                this.ampInterest[i]=Math.round((this.ampRemainingCapital[i]*this.irvaluea)*100000000000)/100000000000;
            }

        //ampHandlingFee
            if(this.kezelesiktsg_type==3){
                //kezelesi koltseg havonta
                this.ampHandlingFee[i]=this.kezelesikoltseghavi;
            } else if(this.kezelesiktsg_type==2){
                //kezelesi koltseg az elso torlesztovel
                if(i==1)this.ampHandlingFee[i]=this.kezelesikoltsega;
            } else {
                //kezelesi koltseg a shopban
            }

        //ampCapital
            if(i==this.futamido+this.postpone){
                this.ampCapital[i]=this.ampRemainingCapital[i];
                //this.ampCapital[i]=Math.round((this.ampRemainingCapital[i])*100000000000)/100000000000;
            } else {
                this.ampCapital[i]=Math.round((this.ampInstalments[i]-this.ampInterest[i]-this.ampHandlingFee[i])*100000000000)/100000000000;
            }
        }
       
        this.thmmensu=this.IRR(this.ampInstalments,0.001);
        this.thm=Math.round((Math.pow((this.thmmensu + 1), 12)-1)*10000)/100;
      
        return this.thm;
                               
        return this.ampInterest;               
                                
        return this.thmmensu+'<br>'+this.thm;
    }      

    this.IRR = function(cashFlow, guess){
        var npv = 0;
        var cnt = 0;
        do {
            npv = this.NPV(guess,cashFlow);
            guess += 0.000001;
            
            if(cnt > this.MAX_IRR_ITERATIONS) return NaN;
            else cnt++;
        } while (npv > 0);
        
        return guess;
    }

    this.NPV = function(discountRate,cashFlow){
        var npv=0;
        for(t=0;t<cashFlow.length;t++){
            npv += cashFlow[t]  / Math.pow((1 + discountRate), t);
        }
        return npv;
    }

    //torlesztoreszletet szamol biztositassal
    this.calculateTorlesztoBiztositassal = function(){
        if(this.kezelesiktsg_type==3){
	    	//ha havi kezelesi koltseg
	        this.torlesztobiztositassal=this.torlesztoalap+this.havibiztositas+this.torlesztopostponed+this.kezelesikoltseghavi;
	        this.torlesztobiztositassalelso=this.torlesztobiztositassal+this.kezelesikoltsegb;
        } else if(this.kezelesiktsg_type==2){
	    	//ha az elso reszlettel fizeti a kezelesi koltseget
	        this.torlesztobiztositassal=this.torlesztoalap+this.havibiztositas+this.torlesztopostponed;
	        this.torlesztobiztositassalelso=this.torlesztobiztositassal+this.kezelesikoltsegb;
        } else {
	    	//ha a boltban fizeti a kezelesi koltseget
	        this.torlesztobiztositassal=this.torlesztoalap+this.havibiztositas+this.torlesztopostponed;
	        this.torlesztobiztositassalelso=this.torlesztobiztositassal;
        }

        return this.torlesztobiztositassalelso;
    }      
    
    //torlesztoreszletet szamol biztositas nelkul
    this.calculateTorlesztoBiztositasnelkul = function(){
        if(this.kezelesiktsg_type==3){
	    	//ha havi kezelesi koltseg
	        this.torleszto=this.torlesztoalap+this.torlesztopostponed+this.kezelesikoltseghavi;
	        this.torlesztoelso=this.torleszto+this.kezelesikoltsega;
        } else if(this.kezelesiktsg_type==2){
	    	//ha az elso reszlettel fizeti a kezelesi koltseget
	        this.torleszto=this.torlesztoalap+this.torlesztopostponed;
	        this.torlesztoelso=this.torleszto+this.kezelesikoltsega;
        } else {
	    	//ha a boltban fizeti a kezelesi koltseget
	        this.torleszto=this.torlesztoalap+this.torlesztopostponed;
	        this.torlesztoelso=this.torleszto;
        }

        return this.torlesztoelso;
    }

    //kiszamol mindent es megjeleniti
    this.reCalculate = function(){
        this.hitelosszeg = parseInt(document.getElementById('credigenFormAr').value.replace(/[^0-9]/g,''),10) - parseInt(document.getElementById('credigenFormOnero').value.replace(/[^0-9]/g,''),10);
        this.futamido = parseInt(document.getElementById('credigenFormFutamido').value,10);
        this.ar = parseInt(document.getElementById('credigenFormAr').value.replace(/[^0-9]/g,''),10);
        this.onero = parseInt(document.getElementById('credigenFormOnero').value.replace(/[^0-9]/g,''),10);
        this.irvaluea = this.kamatlab/100/12;
        this.irvalueb = Math.pow(1+this.irvaluea,this.futamido);
        this.havibiztositas = this.hitelosszeg*(this.biztositas/100);
        if(this.kezelesiktsg_type<3){
            this.kezelesikoltsega = Math.round(this.hitelosszeg*(this.kezelesiktsg_value/100));
            this.kezelesikoltsegb = this.kezelesikoltsega;
        } else {
            this.kezelesikoltseghavi=Math.round(this.hitelosszeg*(this.kezelesiktsg_value/100));                    
        }
        if(this.halasztas-3>0){
			this.postpone = this.halasztas-3;
			if(this.kamatlab>0){
				this.postponeinterest=Math.round(this.hitelosszeg*this.irvaluea*this.postpone);  
				this.torlesztopostponed=Math.round(this.postponeinterest*this.irvalueb*this.irvaluea*(1/(this.irvalueb-1)));  
			} else {
				this.postponeinterest=0;
				this.torlesztopostponed=0;
			}
		} else {
			this.postpone=0
		}
    	if(this.kamatlab>0)this.torlesztoalap=Math.round((this.hitelosszeg-(0/this.irvalueb))*this.irvalueb*this.irvaluea*(1/(this.irvalueb-1)));
    	else this.torlesztoalap=Math.floor(this.hitelosszeg/this.futamido);
        
        this.error='';
        if(this.error=='' && this.ar<=this.onero)this.error='Az önerő nem lehet nagyobb az árnál!';
        if(this.error=='' && this.hitelosszeg<this.hitelosszeg_min)this.error='A hitelösszegnek legalább '+this.hitelosszeg_min+' forintnak kell lennie!';
        if(this.error=='' && this.hitelosszeg>this.hitelosszeg_max)this.error='A hitelösszeg legfeljebb '+this.hitelosszeg_max+' forint lehet!';
        if(this.error=='' && this.onero<Math.round(this.ar*(this.onero_min/100)))this.error='A önerőnek minimum '+this.onero_min+'%-nak kell lennie!';
        if(this.error=='' && this.onero>Math.round(this.ar*(this.onero_max/100)))this.error='A önerő legfeljebb '+this.onero_max+'% lehet!';
        
        if(this.error==''){
        	this.calculateTorlesztoBiztositassal();
        	this.calculateTorlesztoBiztositasnelkul();
        	if(this.kezelesiktsg_type==2){
	            document.getElementById('credigenResultTorlesztoB').innerHTML='<div><b>'+this.formatNumber(this.torleszto,0,'.',' ')+'</b> HUF/hó</div><div style="font-size:9px;">(első részlet: '+this.formatNumber(this.torlesztoelso,0,'.',' ')+')</div>';
	            document.getElementById('credigenResultTorlesztoA').innerHTML='<div><b>'+this.formatNumber(this.torlesztobiztositassal,0,'.',' ')+'</b> HUF/hó</div><div style="font-size:9px;">(első részlet: '+this.formatNumber(this.torlesztobiztositassalelso,0,'.',' ')+')</div>';
	        } else {
	            document.getElementById('credigenResultTorlesztoB').innerHTML='<b>'+this.formatNumber(this.torleszto,0,'.',' ')+'</b> HUF/hó';
	            document.getElementById('credigenResultTorlesztoA').innerHTML='<b>'+this.formatNumber(this.torlesztobiztositassal,0,'.',' ')+'</b> HUF/hó';
	        }
            this.thm=this.calculateTHM();
        	if(this.thm<this.thm_min)this.thm=this.thm_min;
        	if(this.thm>this.thm_max)this.thm=this.thm_max;
            document.getElementById('credigenResultTHM').innerHTML='<b>'+this.thm+'</b> %';
        } else alert(this.error);
    }

    //feltolti a kalkulatort az alapadatokkal
    this.initForm = function(){
        this.onero = Math.round(this.ar*(this.default_onero/100));
        this.hitelosszeg = this.ar - this.onero;
        if(this.hitelosszeg < this.hitelosszeg_min){
            this.onero = Math.round(this.ar*(this.onero_min/100));
            this.hitelosszeg = this.ar - this.onero;
            if(this.hitelosszeg < this.hitelosszeg_min){
                this.ar=500000;
                this.onero = Math.round(this.ar*(this.default_onero/100));
            }
        }
        
        document.getElementById('credigenFormAr').value=this.formatNumber(this.ar,0,'.',' ');
        document.getElementById('credigenFormOnero').value=this.formatNumber(this.onero,0,'.',' ');
        document.getElementById('credigenFormFutamido').value=this.default_futamido;
    }
    
    this.resetResult = function (){
        document.getElementById('credigenResultTorlesztoA').innerHTML='<b>?</b>';
        document.getElementById('credigenResultTorlesztoB').innerHTML='<b>?</b>';
        document.getElementById('credigenResultTHM').innerHTML='<b>?</b>';
    }

    this.formatNumber = function(a, b, c, d) {
    	// formatNumber(number, decimals, comma, formatSeparator)
    	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
    	e = a + '';
    	f = e.split('.');
    	if(!f[0]) f[0] = '0';
    	if(!f[1]) f[1] = '';
    	if(f[1].length < b){
    		g = f[1];
    		for(i = f[1].length + 1; i <= b; i++) {
    			g += '0';
    		}
    		f[1] = g;
    	}
    	if(d != '' && f[0].length > 3) {
    		h = f[0];
    		f[0] = '';
    		for(j = 3; j < h.length; j += 3) {
    			i = h.slice(h.length - j, h.length - j + 3);
    			f[0] = d + i +  f[0] + '';
    		}
    		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
    		f[0] = j + f[0];
    	}
    	c = (b <= 0) ? '': c;
    	return f[0] + c + f[1];
    }

    this.formMoneyFormat = function (ev){
        ev || (ev=window.event);  
        var source;  
        if(typeof ev.target != 'undefined') {  
            source=ev.target;  
        } else if (typeof ev.srcElement != 'undefined') {  
            source=ev.srcElement;  
        } else { return(true); }  
    	
        source.value=this.formatNumber(source.value.replace(/[^0-9]/ig,""),0,'.',' ');
    }

    this.generateID = function(splitter) {
        var sp=(splitter) ? splitter : 'A';
        var now=new Date();
        return Date.parse(now.toGMTString()) + sp + Math.floor(Math.random()*1000000000);
    }
    
    this.writeForm = function(){
        //document.write('<div id="credigenKalkulatorDiv"></div>');

        var htmlFutamidoSelect='<select name="credigenFormFutamido" id="credigenFormFutamido" class="credigenInput" style="font-family:arial; font-size:12px; color:#000000; font-weight:bold; background-color:#ffffff;" onchange="credigen.resetResult();">';
        for(i=this.futamido_min;i<=this.futamido_max;i++)htmlFutamidoSelect+='<option value="'+i+'"'+(i==this.default_futamido?' selected':'')+'>'+i+'</option>';
        htmlFutamidoSelect+='</select>';        
    
        var k = '';
        var boxWidth='228';
        
        k+='<style type="text/css">';
        k+='.credigenLabel {font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left;}';
        k+='.credigenInput {font-family:arial; font-size:12px; color:#000000; font-weight:bold; background-color:#ffffff;}';
        k+='.credigenPadding1 {padding-top:8px;}';
        k+='.credigenButton {font-family:arial; font-size:11px; color:#ffffff; font-weight:bold; background-color:#000000;}';
        k+='.credigenSeparator {height:1px; border-bottom:1px solid #F47B20; padding-top:6px;  font-size:0px;}';
        k+='.credigenResult {font-family:arial; font-size:14px; color:#000000; font-weight:normal; text-align:left;}';
        k+='.credigenLink {font-family:arial; font-size:9px; color:#F47B20; font-weight:normal; text-align:left; text-decoration:none;}';
        k+='.credigenLink:HOVER {color:#F47B20; text-decoration:underline;}';
        k+='.credigenNote {font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left; background-color:#ECECEC;}';
        k+='</style>';        
        
        k+='<div style="width:'+boxWidth+'px; border:0px solid #ECECEC; background-color:#FFFFFF;">';

        k+='<div style="background-color:#ffffff; padding:0px 0px 10px 0px; text-align:center;"><img src="http://www.credigen.hu/pictures/k1/credigen.gif" height="33" width="228" /></div>';
        
        k+='<div style=" padding:0px 5px 10px 5px; text-align:center;">';
        
        //k+='<div class="credigenSeparator" style="height:1px; border-bottom:1px solid #F47B20; padding-top:6px;  font-size:0px;"></div>';
        k+='<div class="credigenLabel credigenPadding1" style="font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left; padding-top:0px;">Vételár:</div><div class="credigenLabel" style="font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left;"><input type="text" id="credigenFormAr" class="credigenInput" style="width:'+(boxWidth-40)+'px; font-family:arial; font-size:12px; color:#000000; font-weight:bold; background-color:#ffffff;" onkeypress="credigen.formMoneyFormat();credigen.resetResult();" onkeyup="credigen.formMoneyFormat();credigen.resetResult();" onfocus="credigen.formMoneyFormat();" onblur="credigen.formMoneyFormat();"> HUF</div>';
        k+='<div class="credigenLabel credigenPadding1" style="font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left; padding-top:8px; display:none;">Önerõ:</div><div class="credigenLabel" style="font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left; display:none;"><input type="text" id="credigenFormOnero" class="credigenInput" style="width:'+(boxWidth-40)+'px; font-family:arial; font-size:12px; color:#000000; font-weight:bold; background-color:#ffffff;" onkeypress="credigen.formMoneyFormat();credigen.resetResult();" onkeyup="credigen.formMoneyFormat();credigen.resetResult();" onfocus="credigen.formMoneyFormat();" onblur="credigen.formMoneyFormat();"> HUF</div>';
        k+='<div class="credigenLabel credigenPadding1" style="font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left; padding-top:8px;">Futamidõ:</div><div class="credigenLabel" style="font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left;">'+htmlFutamidoSelect+' HÓ <input type="button" class="credigenButton" value="Számol" onclick="javascript:credigen.reCalculate();" style="margin-left:10px; font-family:arial; font-size:11px; color:#ffffff; font-weight:bold; background-color:#000000;"></div>';
        k+='<div class="credigenSeparator" style="height:1px; border-bottom:1px solid #F47B20; padding-top:6px;  font-size:0px;"></div>';
        k+='<div class="credigenLabel credigenPadding1" style="font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left; padding-top:8px;">Törlesztõrészlet biztosítással:</div><div id="credigenResultTorlesztoA" class="credigenResult" style="font-family:arial; font-size:14px; color:#000000; font-weight:normal; text-align:left;"></div>';
        k+='<div class="credigenLabel credigenPadding1" style="font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left; padding-top:8px; display:none;">Törlesztõrészlet biztosítás nélkül:</div><div id="credigenResultTorlesztoB" class="credigenResult" style="font-family:arial; font-size:14px; color:#000000; font-weight:normal; text-align:left; display:none;"></div>';
        k+='<div class="credigenLabel credigenPadding1" style="font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left; padding-top:8px;">THM:</div><div id="credigenResultTHM" class="credigenResult" style="font-family:arial; font-size:14px; color:#000000; font-weight:normal; text-align:left;"></div>';
        k+='<div class="credigenSeparator" style="height:1px; border-bottom:1px solid #F47B20; padding-top:6px;  font-size:0px;"></div>';
        k+='<div class="credigenNote credigenPadding1" style="font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left; background-color:#ECECEC; padding-top:8px;">'+text1+'</div>';
       // k+='<div class="credigenLabel credigenPadding1" style="font-family:arial; font-size:9px; color:#797979; font-weight:normal; text-align:left; padding-top:8px;"><a href="'+url+'" target="_blank" class="credigenLink" style="font-family:arial; font-size:9px; color:#F47B20; font-weight:normal; text-align:left; text-decoration:none;">'+urltext+'</a></div>';
        
        k+='</div>';

        k+='</div>';
        k+='<div><img src="http://www.credigen.hu/stat.php?id=56&t='+credigen.generateID()+'" width="1" height="1" border="0"></div>';
        
        document.getElementById('credigenKalkulatorDiv').innerHTML=k;
    }
}

var credigen = new credigenKalkulatorObject("N2502", "HUF", 25, 3, 12, 72, 100000, 1500000, 2700, 96000, 3, 0.2, 0.4, 0, 10, 36, 0, "Személyi kölcsönünket minden 18. életévét betöltött, magyarországi állandó lakhellyel rendelkező személy veheti igénybe, aki jelenlegi munkáltatójánál legalább 6 hónapos munkaviszonnyal és telefonos elérhetőséggel rendelkezik. <b>A fenti adatok tájékoztató jellegűek!</b> N2502", "www.credigen.hu", "http://www.credigen.hu", 32.15, 33.32);

if(document.getElementById('credigenprice') != null)credigen.ar = document.getElementById('credigenprice').innerHTML.replace(/[^0-9]/g,'');
else credigen.ar = 500000;
tmp=credigen.writeForm();
credigen.initForm();
credigen.reCalculate();

*/
