///////////////////////////////CIB HITELSZAMOLAS///////////////////////////////
////////////////////////Written by: CoL (col@mezon.net)////////////////////////
//////////////////////////////////2000.03.02///////////////////////////////////
////Beállítások

var pVetelar={'min':1000000, 'max':12500000, 'step':100000};
var pOnero={'min':0, 'max':12500000, 'step':100000};
var pHitelosszeg={'min':1000000, 'max':12500000, 'step':100000};
var pFutamido={'min':1, 'max':35, 'step':1};
var in_goal=1;

var szorzo =365/360;

var min_futamido=pFutamido.min*12; //ho
var max_futamido=pFutamido.max*12; //ho
var kezelesi_kamat=0.0/100; //kezelesi koltseg kamat merteke
var min_igenyelheto_hitel=pHitelosszeg.min;
var max_igenyelheto_hitel=pHitelosszeg.max;

var hav = new Array(Math.round(max_futamido/12));
j=0;
for(i=12;i<=max_futamido;i=i+6) hav[j++]=i;


function setRate() 
{
	if (document.getElementById('in_goal').selectedIndex == 0)
	{
		document.getElementById('td_house').style.display='block';
		showHouse=true;
		document.getElementById('eves_kamat').value = '6.00';
		document.getElementById('td_vetelar').style.display=tableRowDisplayType;
		document.getElementById('td_onero').style.display=tableRowDisplayType;
		document.getElementById('td_gyermekek').style.display=tableRowDisplayType;
		max_igenyelheto_hitel=pHitelosszeg.max=12500000;
		$("#hitelosszeg").slider("option", "max", pHitelosszeg.max);
		document.cibform.in_havitorleszto.value=0;
		setSlider('vetelar', pVetelar.min);
		setSlider('onero', 0);
		$("#gyermekek_i").val(aGyermekekSzama[0]);
		$("#gyermekek").slider("value", 0);
		setTimeout("updateHouse()",100);
	}
	else
	{
		document.getElementById('td_house').style.display='none';
		showHouse=false;
		document.getElementById('eves_kamat').value = '6.26';
		document.getElementById('td_vetelar').style.display='none';
		document.getElementById('td_onero').style.display='none';
		document.getElementById('td_gyermekek').style.display='none';
		max_igenyelheto_hitel=pHitelosszeg.max=5000000;
		$("#hitelosszeg").slider("option", "max", pHitelosszeg.max);
	}

	document.cibform.in_havitorleszto.value=0;
	setSlider('hitelosszeg', pHitelosszeg.min);
	setSlider('futamido', pFutamido.min);
}

///////////////////////////////////////////////////////////////////////////////
function szamol()
{
	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 in_h1 = document.cibform.in_hitel.value.numValue();
	var in_t1 = document.cibform.in_havitorleszto.value.numValue();

	eves_kamat=document.cibform.eves_kamat.value/100;
	kamat=(eves_kamat/12)*szorzo;

	var in_f1=document.cibform.in_futamido.value.numValue()*12;
	var kezelesi_koltseg=(in_h1*kezelesi_kamat)/12	//kezelesi koltseg

	if(isNaN(in_t1) && isNaN(in_h1) && isNaN(in_f1))
	{
		alert(error2)
		return;
	}

	else if(isNaN(in_t1) && isNaN(in_h1))
	{
		alert(error2)
		return;
	}
	else if(isNaN(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=ujotthon&in_t1=' + in_t1 + '&in_t1_f=' + in_t1_f + '&in_h1=' + in_h1 + '&in_f1=' + in_f1 + '&eves_kamat=' + eves_kamat + '&eves_kamat_f=' + eves_kamat_f;
///////////////////////////////////////
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*1))
	{
		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_igenyelhetohitel=in_h1;

	out_havitorleszto = out_havitorleszto + kezelesi_koltseg;

	t_h_v = out_havitorleszto * 0.076; //teljes hitelfedezeti vedelem
	e_h_v = out_havitorleszto * 0.032; //egyszerusitett hitelfedezeti vedelem
	h_m_cs = out_havitorleszto * 0.044; //Bármely okú halál és 100%-os munkaképesség csökkenés
	b_h = out_havitorleszto * 0.005; //Baleseti halál 
	k_k = out_havitorleszto * 0.012; //Keresoképtelenség
	m_n = out_havitorleszto * 0.020; //Munkanélküliség

	output(out_igenyelhetohitel,out_havitorleszto,t_h_v,e_h_v,h_m_cs,b_h,k_k,m_n) //kiiratas

}

///////////////////////////////////////
if(isNaN(out_havitorleszto) || isNaN(out_igenyelhetohitel)) 
{ 
	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';

}//szamol vege
//////////////////////////////////////////////////////////////////////////////
function output(h,t,t_h_v,e_h_v,h_m_cs,b_h,k_k,m_n)
{
	document.cibform.in_havitorleszto.value=addCommas(egesz(t));
	document.cibform.in_hitel.value=addCommas(egesz(h));

/*
	document.cibform.t_h_v.value = addCommas(Math.round(t_h_v));
	document.cibform.e_h_v.value = addCommas(Math.round(e_h_v));
	document.cibform.h_m_cs.value = addCommas(Math.round(h_m_cs));
	document.cibform.b_h.value = addCommas(Math.round(b_h));
	document.cibform.k_k.value = addCommas(Math.round(k_k));
	document.cibform.m_n.value = addCommas(Math.round(m_n));
*/
}

//////////////////////////////////////////////////////////////////////////////
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 showVVTable() 
{
	document.getElementById('table_3').style.display='block';
}

function initCalc()
{
	szamol();
	updateHouse();
}
