///////////////////////////////CIB HITELSZAMOLAS///////////////////////////////
////////////////////////Written by: CoL (col@mezon.net)////////////////////////
//////////////////////////////////2000.03.02///////////////////////////////////
////Beállítások

var pHitelosszeg={'min':200000, 'max':3000000, 'step':100000};
var pFutamido={'min':1, 'max':6, 'step':1};
var showHouse = false;

var currency = 1;

var szorzo =365/360;

var min_futamido=pFutamido.min*12; //ev
var max_futamido=pFutamido.max*12; //ev
var min_igenyelheto_hitel=pHitelosszeg.min;
var mid_igenyelheto_hitel=pHitelosszeg.max;
var max_igenyelheto_hitel=pHitelosszeg.max;
var kk_rate_1=1/100; //Egyszeri kezelési költség egyéb esetben: 1,00%
var kk_rate_2=1.5/100; //Egyszeri kezelési költség postai készpénz-átutalási megbízás nyomtatás esetén: 1,50%

var hav=new Array(12,18,24,30,36,42,48,54,60,66,72);

var error1="Kérjük adjon meg helyes értékeket a mezôkben!"
var error2="Kérjük töltse ki a megfelelô  2 mezôt!"
var error3="Kérjük egyszerre csak 2 mezôt töltsön ki!"
var error4="A minimálisan igényelhetô hitel:"+min_igenyelheto_hitel+" Ft";
var error4b="A maximálisan igényelhetô hitel:"+max_igenyelheto_hitel+" Ft";
var error5="A minimális futamidô:"+min_futamido/12+" év ("+min_futamido+" hónap)";
var error6="A maximális futamidô:"+max_futamido/12+" év ("+max_futamido+" hónap)";
var error7="Kérjük töltse ki a futamidô mezôt!"
var error8='A futamidô csak féléves bontásban lehetséges!'+'\n         Pl.:'+hav[0]+' hó, '+hav[1]+' hó, '+hav[2]+' hó,' +hav[3]+' hó, '+hav[4]+' hó ...';
    error8+='\nMinimális futamidô: '+min_futamido+' hó. Maximális futamidô: '+max_futamido+' hó.';
var error9="3 millió forint alatti hiteligénylés esetén, a kamat 18.99% vagy 22.99% lehet!";
var error10="3 millió forint feletti hiteligénylés esetén, a kamat 15.99% vagy 19.99% lehet!";


///////////////////////////////////////////////////////////////////////////////
function szamol()
{
	
	var in_kam=(document.cibform.first_rate_display.value)/100;
	var in_kam_2=(document.cibform.second_rate_display.value)/100;

	var in_t1=document.cibform.in_havitorleszto.value.numValue();
	var in_h1=document.cibform.in_hitel.value.numValue();
	var in_f1=document.cibform.in_futamido.value*12;

	var kamat=(in_kam/12)*szorzo;
	var kamat_2=(in_kam_2/12)*szorzo;

	if(in_t1=="" && in_h1=="" && in_f1=="")
	{
		alert(error2)
		return;
	}
	else if(in_t1=="" && in_h1=="")
	{
		alert(error2)
		return;
	}
	else if(in_f1=="")
	{
		alert(error7)
		return;
	}

	var hav_ok=0;

	for(var i=0;i<hav.length;i++)
	{
		if((in_f1)==hav[i])
		{ 
			hav_ok=1; 
		}
	}

	if(hav_ok!=1) 
	{  
		alert(error8);
		document.cibform.in_futamido.value=min_futamido;
		in_f1=min_futamido;
	}
	
//frames["statFrame"].location = '/system/stat?resource=calc&type=lsz&in_t1=' + in_t1 + '&in_h1=' + in_h1 + '&in_f1=' + in_f1 + '&in_kam=' + in_kam;
///////////////////////////////////////
	if((in_h1!=0) && (in_f1!=0)) //a hitelbol es a futamidobol a torleszto
	{
		if(in_h1<min_igenyelheto_hitel)
		{
			alert(error4)
			in_h1=document.cibform.in_hitel.value=min_igenyelheto_hitel
		}
		else if(in_h1>max_igenyelheto_hitel)
		{
			alert(error4b)
			in_h1=document.cibform.in_hitel.value=max_igenyelheto_hitel
		}
		
		if(in_f1<min_futamido)
		{
			alert(error5)
			in_f1=document.cibform.in_futamido.value=min_futamido
		}
		else if(in_f1>max_futamido)
		{
			alert(error6)
			in_f1=document.cibform.in_futamido.value=max_futamido
		}
	var out_havitorleszto=sz_havitorleszto(in_h1,in_f1,kamat);
	var out_havitorleszto_2=sz_havitorleszto(in_h1,in_f1,kamat_2);
	var out_igenyelhetohitel=in_h1;
	var out_kk_1 = in_h1 * kk_rate_1;
	var out_kk_2 = in_h1 * kk_rate_2;

	var h_v = out_havitorleszto * 0.06; //hitelfedezeti vedelem
	var h_v_2 = out_havitorleszto_2 * 0.06; //hitelfedezeti vedelem b minosites eseten
	output(out_igenyelhetohitel,out_havitorleszto,out_havitorleszto_2,h_v,h_v_2,out_kk_1,out_kk_2) //kiiratas
	}

///////////////////////////////////////

	if(in_t1!=0 && in_f1!=0 && !((in_h1!=0) && (in_f1!=0))) //a torlesztobol es a futamidobol a hitel
	{
		if(in_f1<min_futamido)
		{
			alert(error5)
			in_f1=document.cibform.in_futamido.value=min_futamido
		}
		else if(in_f1>max_futamido)
		{
			alert(error6)
			in_f1=document.cibform.in_futamido.value=max_futamido
		}
		
		var min_t1=egesz(sz_havitorleszto(min_igenyelheto_hitel,min_futamido,kamat));
		var max_t1=egesz(sz_havitorleszto(max_igenyelheto_hitel,max_futamido,kamat));
		var akt_min_t1=egesz(sz_havitorleszto(min_igenyelheto_hitel,in_f1,kamat));
		var akt_max_t1=egesz(sz_havitorleszto(max_igenyelheto_hitel,in_f1,kamat));

		var akt_out_igenyelhetohitel=egesz(sz_hitel(in_t1,in_f1,kamat));

		if(min_t1==in_t1) 
		{ 
			akt_out_igenyelhetohitel=min_igenyelheto_hitel; 
		}

		if(akt_out_igenyelhetohitel<min_igenyelheto_hitel)
		{
			 alert("A minimális törlesztôrészlet:"+egesz(akt_min_t1))
			 in_t1=document.cibform.in_havitorleszto.value=egesz(akt_min_t1);
			 var out_havitorleszto=in_t1;
			 var out_igenyelhetohitel=min_igenyelheto_hitel;
		}
		else if(akt_out_igenyelhetohitel>max_igenyelheto_hitel)
		{
			 alert("A maximális törlesztôrészlet:"+egesz(akt_max_t1))
			 in_t1=document.cibform.in_havitorleszto.value=egesz(akt_max_t1);
			 var out_havitorleszto=in_t1;
			 var out_igenyelhetohitel=max_igenyelheto_hitel;
		}
		else
		{
			var out_havitorleszto=in_t1;
			var out_igenyelhetohitel=sz_hitel(in_t1,in_f1,kamat);
			var out_igenyelhetohitel_2=sz_hitel(in_t1,in_f1,kamat_2);

			var out_havitorleszto_2=sz_havitorleszto(out_igenyelhetohitel,in_f1,kamat_2);

			if(in_t1==akt_min_t1) { out_igenyelhetohitel=min_igenyelheto_hitel; }
			if(in_t1==akt_max_t1) { out_igenyelhetohitel=max_igenyelheto_hitel; }

			var out_kk_1 = out_igenyelhetohitel * kk_rate_1;
			var out_kk_2 = out_igenyelhetohitel * kk_rate_2;
		}

		h_v = out_havitorleszto * 0.06; //hitelfedezeti vedelem
		h_v_2 = out_havitorleszto_2 * 0.06; //hitelfedezeti vedelem b adosminosites eseten

		output_2(out_igenyelhetohitel,out_igenyelhetohitel_2,out_havitorleszto,out_havitorleszto_2,h_v,h_v_2,out_kk_1) //kiiratas
	}

///////////////////////////////////////

	if(isNaN(out_havitorleszto) || isNaN(out_igenyelhetohitel)) 
	{ 
		alert(out_havitorleszto);
		alert(error1) 
		document.cibform.in_havitorleszto.value = ""; 
		document.cibform.in_hitel.value = ""; 
		document.cibform.in_futamido.value= "";
		return;
	}

	document.getElementById('changeable_values').style.display = 'block';
	document.getElementById('first_rate').value = document.cibform.first_rate_display.value;
	document.getElementById('second_rate').value = document.cibform.second_rate_display.value;
}//szamol vege
//////////////////////////////////////////////////////////////////////////////
function output(h,t,t2,h_v,h_v_2,kk_1)
{
	document.cibform.in_havitorleszto.value=addCommas(egesz(t));
	document.cibform.in_havitorleszto_2.value=addCommas(egesz(t2));
	document.cibform.in_hitel.value=addCommas(egesz(h));
	document.getElementById('h_v').innerHTML = addCommas(Math.round(h_v),3,0);
	document.getElementById('h_v_2').innerHTML = addCommas(Math.round(h_v_2),3,0);
	document.cibform.kk_1.value=egesz(kk_1);
}
//////////////////////////////////////////////////////////////////////////////
function output_2(h,h2,t,t2,h_v,h_v_2,kk_1)
{
	document.cibform.in_havitorleszto.value=egesz(t);
	document.cibform.in_havitorleszto_2.value=egesz(t2);
	document.cibform.in_hitel.value=egesz(h);
	document.cibform.in_hitel_2.value=egesz(h2);
	document.cibform.in_hv.value=egesz(h_v);
	document.cibform.in_hv_b.value=egesz(h_v_2);
	document.cibform.kk_1.value=egesz(kk_1);
}
//////////////////////////////////////////////////////////////////////////////
function sz_havitorleszto(h,f,k)
{
	return h*hatvany((1+k),f)*k/(hatvany((1+k),f)-1) ;
}
//////////////////////////////////////////////////////////////////////////////
function sz_hitel(t,f,k)
{
	return t/(hatvany((1+k),f)*k/(hatvany((1+k),f)-1)) ;
}
//////////////////////////////////////////////////////////////////////////////
function hatvany(x,y)
{
	return Math.pow(x,y)
}
//////////////////////////////////////////////////////////////////////////////
function egesz(x)
{
	return Math.round(x)
}
//////////////////////////////////////////////////////////////////////////////
function showTable() 
{
	document.getElementById('table_2').style.display='block';
}
function initCalc()
{
	szamol(); showTable();
}


