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));
}

// bool ako je true onda formatira na 2 decimale
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)
{
	//ovo je potrebno ako bi se odustalo od dom nacina citanja vrednosti elemenata forme
	//eval('var forma=document.'+NazivForme+';');

	var xval=document.getElementById("MetodObracuna");
	var broj=xval.selectedIndex;
	this.metod=xval[broj]+'konf';
	
	this.kredit=this.makeNum(document.getElementById('TrazeniKredit').value);
	this.troskovi=parseFloat(this.kredit*parseFloat(this.makeNum(document.getElementById('NaknadaPosto').value))/100);
	//alert(this.troskovi);
	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);
	//obrada unosa korisnika
	var broj=document.getElementById('GodisnjaStopa').value;
	broj=broj.replace(".",",");
	
	//this.Gkamata=this.makeNum(document.getElementById('GodisnjaStopa').value);
	document.getElementById('GodisnjaStopa').value=broj;
	this.Gkamata=this.makeNum(broj);
	this.depozitStopa=this.makeNum(document.getElementById('DepozitStopa').value);
	
	//alert('kredit='+this.kredit+' troskovi='+this.troskovi+' depozit='+this.depozit);
}

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;
}

//deo koda koji ponistava sve u formi
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('depozitRc').value='0,00';
	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('iPovracajDepozita').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(".",",");
	//alert(this.troskovi);
	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(".",",");
	//alert(this.troskovi);
	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==''))
	{
		if(this.jezik=='sr')
		{
			alert("Unesite godisnju 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);
		this.PrikaziAnuitetniPlan("anuitetniPlan");
	}
}

LKalk.prototype.PrikaziAnuitetniPlan=function(KontejnerPrikaza)
{
	var out = "";
	
	out += "<table cellspacing=1 cellpadding=1 bgcolor=#999999>";
	out += "<tr bgcolor=#c3ff8d class=sivi>";
	if(this.jezik=='sr')
	{
		out += "<td class=plava><span class=whtxtsmall>&nbsp;Br&nbsp;</span></td>";
		out += "<td class=plava><span class=whtxtsmall>&nbsp;Isplata&nbsp;<br>&nbsp;kredita&nbsp;</span></td>";
		out += "<td class=plava><span class=whtxtsmall>&nbsp;Anuitet&nbsp;</span></td>";
		out += "<td class=plava><span class=whtxtsmall>&nbsp;Otplata&nbsp;</span></td>";
		out += "<td class=plava><span class=whtxtsmall>&nbsp;Uplata&nbsp;<br>&nbsp;kamate&nbsp;</span></td>";
		out += "<td class=plava><span class=whtxtsmall>&nbsp;Druge&nbsp;<br>&nbsp;uplate&nbsp;</span></td>";
		out += "<td class=plava><span class=whtxtsmall>&nbsp;Stanje&nbsp;<br>&nbsp;kredita&nbsp;</span></td>";
	}

	out += "</tr>";
	
	out += "<tr bgcolor=#ffffff>";
	out += "<td class=svplava align=right>0&nbsp;&nbsp;</td>";
	out += "<td class=svplava align=right>" + this.roundAndFormat(this.kredit, true) + "</td>";
	out += "<td class=svplava></td>";
	out += "<td class=svplava></td>";
	out += "<td class=svplava></td>";
	out += "<td class=svplava align=right>" + this.roundAndFormat(this.ucesce+this.troskovi, true) + "</td>";
	out += "<td class=svplava align=right>" + this.roundAndFormat(this.kredit-this.ucesce, true) + "</td>";
	
	out += "</tr>";
	
	var stanje = this.kredit-this.ucesce;
	var kamata;
	var otplata;
	
	
	for (i=0; i<this.period-1; i++)
	{
		kamata =this.IzracunajKamatuOstatkaDuga(stanje);
		otplata =this.IzracunajOplatuZaMesec(stanje);
		stanje = this.IzracunajNoviOstatakDuga(stanje);
		
		out += "<tr bgcolor=#ffffff>";
		out += "<td class=svplava align=right>"+(i+1)+"&nbsp;&nbsp;</td>";
		out += "<td class=svplava></td>";
		out += "<td class=svplava align=right>" + this.roundAndFormat(this.anuitet, true) + "</td>";
		out += "<td class=svplava align=right>" + this.roundAndFormat(otplata, true) + "</td>";
		out += "<td class=svplava align=right>" + this.roundAndFormat(kamata, true) + "</td>";
		out += "<td class=svplava></td>";
		out += "<td class=svplava align=right>" + this.roundAndFormat(stanje, true) + "</td>";
	
		out += "</tr>";
		
	  	
	}

	kamata =this.IzracunajKamatuOstatkaDuga(stanje);
	otplata =this.IzracunajOplatuZaMesec(stanje);
	
	stanje = 0;
	
	
	out += "<tr bgcolor=#ffffff>";
	out += "<td class=svplava align=right>"+(i+1)+"&nbsp;&nbsp;</td>";
	out += "<td class=svplava></td>";
	out += "<td class=svplava align=right>" + this.roundAndFormat(this.anuitet, true) + "</td>";
	out += "<td class=svplava align=right>" + this.roundAndFormat(otplata, true) + "</td>";
	out += "<td class=svplava align=right>" + this.roundAndFormat(kamata, true) + "</td>";
	out += "<td class=svplava></td>";
	out += "<td class=svplava align=right>" + this.roundAndFormat(stanje, true) + "</td>";
	
	out += "</tr>";
	
	
	
	out += "</table>";
	
	document.getElementById(KontejnerPrikaza).innerHTML=out;
	
}
