

function get_radio_value()
{
for (var i=0; i < document.kredit_calc.MetodObracunaradio.length; i++)
   {
   if (document.kredit_calc.MetodObracunaradio[i].checked)
      {
      var rad_val = document.kredit_calc.MetodObracunaradio[i].value;
	  return(rad_val);
      }
   }
}
function LKalk(objIme)
{
	this.obj=objIme;
	this.period=12;
	this.kredit=10000;
	this.ucesce=20;
	this.troskovi=1;
	this.depozit=0;
	this.depozitStopa=0;
	this.Gkamata=0;
	this.Mkamta=0;
	this.Mkoeficient=0;
	this.anuitet=0;
	this.metod='konf';
	this.stanje=0;
	this.jezik='sr';
}

LKalk.prototype.formatNumberString=function( u_value, bool )
{
	var i;
	var decCtr=0;
	var decIndex;
	var decIncr = 3;
	var str = u_value+"";
	var strlen = str.length;
	
	decIndex = str.indexOf('.');
	
	str=str.replace(".",",");
	
	if ( decIndex == -1 )
		decIndex = strlen;
	
	for ( i = decIndex-1; i > 0; i-- )
	{
		decCtr++;
		if ( decCtr == 3 )
		{
			str = str.substring(0,i) + "." + str.substring(i, strlen );
			strlen++;
			decCtr = 0;
		}
	}
	
	
	if( str.indexOf(',') == -1 && bool )
		str += ',00';
	else if( str.indexOf(',')+2 == str.length && bool )
		str += '0';
	return(str);
}

LKalk.prototype.makeTxtValid=function( val, bool )
{
	var str, strchar;
	var tempstr = '';
	
	str = val;
	while( str.indexOf(',') != str.lastIndexOf(',') )
		str = str.substring(0,str.indexOf(',')) + str.substring(str.indexOf(',')+1, str.length );

	for( i=0; i<str.length; i++)
	{
		strchar = str.charAt(i);
		if( (strchar == ',') || (strchar == '0') || (strchar == '1') || (strchar == '2')	|| (strchar == '3') || (strchar == '4') || (strchar == '5') || (strchar	==	'6') || (strchar == '7') || (strchar == '8') || (strchar == '9') )
			tempstr = tempstr + strchar;
	}
	
	return( this.roundAndFormat( this.makeNum( tempstr ), bool ) );
}

LKalk.prototype.makeNum=function( val )
{
	while( val.indexOf('.') != -1 )
		val = val.replace('.', '');
		
	if( val.indexOf(',') != -1 )
		val = val.substring( 0,val.indexOf(',') ) + '.' + val.substring( val.indexOf(',')+1, val.length );
		
	return (parseFloat(val));
}

LKalk.prototype.roundAndFormat=function( val, bool )
{
	val = this.roundNum( val );
	return ( this.formatNumberString( val, bool ) );
}

LKalk.prototype.roundNum=function( val )
{
	return Math.round( parseFloat(val)*100 )/100;
}



LKalk.prototype.DodeliParametre=function(Kredit,Period,Ucesce,Depozit,GodisnjaKamata,Troskovi)
{
	this.kredit=parseFloat(Kredit);
	this.period=parseInt(Period);
	this.ucesce=parseFloat(Ucesce);
	this.depozit=parseFloat(Depozit);
	this.Gkamata=parseFloat(GodisnjaKamata);
	this.troskovi=parseFloat(Troskovi);
}

LKalk.prototype.UcitajParametreIzForme=function(NazivForme)
{

	this.metod=get_radio_value();

	
	this.kredit=this.makeNum(document.getElementById('TrazeniKredit').value);
	this.troskovi=parseFloat(this.kredit*parseFloat(this.makeNum(document.getElementById('NaknadaPosto').value))/100);
	this.ucesce=parseFloat(this.kredit*parseFloat(this.makeNum(document.getElementById('UcescePosto').value))/100);
	this.depozit=parseFloat(this.kredit*parseFloat(this.makeNum(document.getElementById('DepozitPosto').value))/100);
	this.period=parseInt(document.getElementById('Period').value);
	var broj=document.getElementById('GodisnjaStopa').value;
	broj=broj.replace(".",",");
	
	document.getElementById('GodisnjaStopa').value=broj;
	this.Gkamata=this.makeNum(broj);
	this.depozitStopa=this.makeNum(document.getElementById('DepozitStopa').value);
	
}

LKalk.prototype.DodeliPeriod=function(Vrednost)
{
	this.period=parseFloat(Vrednost);
}

LKalk.prototype.DodeliJezik=function(Vrednost)
{
	this.jezik=Vrednost;
}

LKalk.prototype.DodeliKredit=function(Vrednost)
{
	this.kredit=parseFloat(Vrednost);
}

LKalk.prototype.DodeliUcesce=function(Vrednost)
{
	this.ucesce==parseFloat(this.Kredit*parseFloat(Vrednost)/100);
}

LKalk.prototype.DodeliDepozit=function()
{
	this.depozit==parseFloat(Vrednost);
}

LKalk.prototype.DodeliMetod=function()
{
	this.metod=Vrednost;
}

LKalk.prototype.ResetujFormu=function()
{
	document.getElementById('TrazeniKredit').value='10.000,00';
	document.getElementById('NaknadaPosto').value='0';
	document.getElementById('naknadaRc').value='0,00';
	document.getElementById('UcescePosto').value='0';
	document.getElementById('ucesceRc').value='0,00';
	document.getElementById('DepozitPosto').value='0';
	document.getElementById('DepozitStopa').value='0';
	document.getElementById('Period').value='12';
	document.getElementById('GodisnjaStopa').value='0';
	document.getElementById('iEfektivnaKamata').value='';
	document.getElementById('ivAnuitet').value='';
	document.getElementById('ikamata').value='';
	document.getElementById('iplacanja').value='0';
//	document.getElementById('anuitetniPlan').innerHTML='';

}

LKalk.prototype.IzracunajNaknadu=function()
{
	if(document.getElementById('NaknadaPosto').value=='')
	{
		document.getElementById('NaknadaPosto').value='0,00';
	}
	var broj=document.getElementById('NaknadaPosto').value;
	broj=broj.replace(".",",");
	document.getElementById('naknadaRc').value=this.roundAndFormat(this.makeNum(document.getElementById('TrazeniKredit').value)*parseFloat(this.makeNum(broj))/100);
	document.getElementById('NaknadaPosto').value=this.roundAndFormat(this.makeNum(broj));
	
}

LKalk.prototype.IzracunajUcesce=function()
{
	if(document.getElementById('UcescePosto').value=='')
	{
		document.getElementById('UcescePosto').value='0,00';
	}
	var broj=document.getElementById('UcescePosto').value;
	broj=broj.replace(".",",");
	document.getElementById('ucesceRc').value=this.roundAndFormat(this.makeNum(document.getElementById('TrazeniKredit').value)*parseFloat(this.makeNum(broj))/100);
document.getElementById('UcescePosto').value=this.roundAndFormat(this.makeNum(broj));
	
	
}

LKalk.prototype.IzracunajDepozit=function()
{
	if(document.getElementById('DepozitPosto').value=='')
	{
		document.getElementById('DepozitPosto').value='0,00';
	}
var broj=document.getElementById('DepozitPosto').value;
	broj=broj.replace(".",",");
	document.getElementById('depozitRc').value=this.roundAndFormat(this.makeNum(document.getElementById('TrazeniKredit').value)*parseFloat(this.makeNum(broj))/100);
document.getElementById('DepozitPosto').value=this.roundAndFormat(this.makeNum(broj));
	
	this.IzracunajDepozitStopu();
}

LKalk.prototype.IzracunajDepozitStopu=function()
{
	if(document.getElementById('DepozitStopa').value=='')
	{
		document.getElementById('DepozitStopa').value='0,00';
	}
var broj=document.getElementById('DepozitStopa').value;
	broj=broj.replace(".",",");
	document.getElementById('depozitStopaRc').value=this.roundAndFormat(this.makeNum(document.getElementById('depozitRc').value)*parseFloat(this.makeNum(broj))/100);
document.getElementById('DepozitStopa').value=this.roundAndFormat(this.makeNum(broj));
	
	
}

LKalk.prototype.MesStopa = function( val )
{
	return  Math.pow( val/100 + 1, 1/12 );
}

LKalk.prototype.IzracunajDepozitNaKraju=function()
{
	var depozitNaKraju = this.depozit * Math.pow(this.MesStopa(this.depozitStopa),this.period);
	return depozitNaKraju;
}

LKalk.prototype.DodeliKamatu=function(Vrednost)
{
	this.Gkamata=parseFloat(Vrednost);	
}

LKalk.prototype.IzracunajMKamatu=function()
{
	if(this.metod=='konf')
	{
		this.Mkamata=100*(Math.pow((1+parseFloat(this.Gkamata)/100),(1/12))-1);
		this.Mkoeficient=1+this.Mkamata/100;
	}
	else
	{
		this.Mkamata=parseFloat(parseFloat(this.Gkamata)/12);
		this.Mkoeficient=1+this.Mkamata/100;
	}
}



LKalk.prototype.IzracunajUkupnuKamatu=function()
{

	return this.anuitet*(this.period)-(this.kredit-this.ucesce);
}

LKalk.prototype.IzracunajUkupnoPlacanje=function()
{

	var vrednost=this.anuitet*this.period+this.ucesce+this.troskovi;
	return vrednost;
}

LKalk.prototype.IzracunajAnuitet=function()
{
	
	this.IzracunajMKamatu();
	
	var kreditManjeUcesce=this.kredit-this.ucesce;
	this.anuitet=kreditManjeUcesce*(Math.pow(this.Mkoeficient,this.period))*(this.Mkoeficient-1)/(Math.pow(this.Mkoeficient,this.period)-1)
}

LKalk.prototype.IzracunajKamatuOstatkaDuga=function(OstatakDuga)
{
	return parseFloat(OstatakDuga*this.Mkamata/100);
}

LKalk.prototype.IzracunajOplatuZaMesec=function(OstatakDuga)
{
	return parseFloat(this.anuitet-this.IzracunajKamatuOstatkaDuga(OstatakDuga));
}

LKalk.prototype.IzracunajNoviOstatakDuga=function(OstatakDuga)
{
	var ostduga=OstatakDuga-this.IzracunajOplatuZaMesec(OstatakDuga);
	return ostduga;
}

LKalk.prototype.PrikaziOsnovnePodatke=function()
{
	
	if ((document.getElementById('GodisnjaStopa').value==0)||(document.getElementById('GodisnjaStopa').value==''))
	{
			alert("Morate uneti godišnju kamatnu stopu");
	}
	else
	{
		this.UcitajParametreIzForme('test');
		this.IzracunajAnuitet();
		document.getElementById('iEfektivnaKamata').value=this.roundAndFormat(this.Mkamata,true);
//		document.getElementById('iPovracajDepozita').value=this.roundAndFormat(this.IzracunajDepozitNaKraju(),true);
		document.getElementById('ivAnuitet').value=this.roundAndFormat(this.anuitet,true);
		document.getElementById('ikamata').value=this.roundAndFormat(this.IzracunajUkupnuKamatu(),true);
		document.getElementById('iplacanja').value=this.roundAndFormat(this.IzracunajUkupnoPlacanje(),true);
	}
}
