// (c) 2008-2009 Werner Zauzig

//var svid='tg';

function TagesgeldDaten(csv){
	var i=0;
	this.ID=csv[i++];
	i++;this.aArt="Tagesgeld";//"T"
	this.name=str_replace("&"," & ",csv[i++]);
	this.z=number_check(csv[i++],0);
	this.z10=number_check(csv[i++],0);
	this.z12=number_check(csv[i++],0);
	this.z25=number_check(csv[i++],0);
	this.z50=number_check(csv[i++],0);
	this.z100=number_check(csv[i++],0);
	this.z200=number_check(csv[i++],0);
	this.anzZinstermine=number_check(csv[i++],1);
	this.formel=csv[i++].trim();
	this.mind=number_check(csv[i++],0);
	this.max=number_check(csv[i++]);
	this.es=csv[i++];
	this.einmGebuehr=number_check(csv[i++],0);
	this.mtlGebFormel=csv[i++].trim();
	this.produktname=csv[i++];
	this.fuer=csv[i++];
	this.urlid=csv[i++];

	//var anbtyp=csv[i++];
	//this.fuerAlle=(anbtyp.indexOf("0")>0?0:1);
	if(!this.einmGeb)this.einmGeb=0;
	this.mtlGeb=0;
	this.info="";
}

TagesgeldDaten.prototype.GetFormelErgebnis=function(formel,betrag,lzj,anz){
	if(formel==null||formel=="")return 0;
	function sel(){
		for(var i=0;i<sel.arguments.length-1;i+=2)
			if(sel.arguments[i])return sel.arguments[i+1];
		return (i<sel.arguments.length?sel.arguments[i]:"");
	}
	var f=str_replace(",",".",str_replace(".","",formel));
	f=str_replace(";",",",f);
	f=str_replace("#",betrag,f);
	f=str_replace("~",lzj,f);
	f=str_replace("^",lzj*12,f);
	f=str_replace("@",anz,f);
//alert("GetFormelErgebnis("+formel+","+betrag+","+lzj+","+anz+")="+f+"="+eval(f));
	return eval(f);
}

TagesgeldDaten.prototype.GetGebuehren=function(lzm){
	this.mtlGeb=this.GetFormelErgebnis(this.mtlGebFormel);
	if(this.mtlGeb<0)
		this.mtlGeb=0;
	if(this.einmGeb<=0)this.einmGeb=0;
	var geb=this.einmGeb+this.mtlGeb*lzm;
//alert("GetGebuehren("+lzm+"): einm="+this.einmGeb+" + mtl="+this.mtlGeb+"="+geb);
	return geb;
}

TagesgeldDaten.prototype.SetErgebnis=function(betrag,lzm){
	betrag=1*betrag;
	lzm=1*lzm;
	this.zins=this.endbetrag="\u00a0";
	if(this.fuer!=""){
		var s=str_replace("Privatpersonen","",str_replace("nur","",this.fuer)).trim();
//alert("s.charAt(0)="+s.charAt(0)+" s.substr(1)="+s.substr(1)+" s="+s);
		if(s.charAt(0)==",")s=s.substr(1).trim();
//		if(s!="")this.info="auch "+s;
	}
//alert("betrag="+betrag+": mind="+this.mind+" max="+this.max);
	if(betrag<this.mind){
		this.info+=(this.info?", ":"")+"Anlage mind. "+number_format(this.mind,0)+"\u00a0€";
		return 0;
	}
	if(betrag>this.max && this.max>0){
		this.info+=(this.info?", ":"")+"Anlage max. "+number_format(this.max,0)+"\u00a0€";
		return 0;
	}
	var lzj=lzm/12.0;
//alert("lzm="+lzm+": lzj="+lzj+" formel="+this.formel);
	var z;
	if(this.formel!="")
		z=number_format(100*this.GetFormelErgebnis(this.formel,betrag,lzj,1)/betrag,3);
	else if(betrag>=200000&&this.z200>0)z=this.z200;
	else if(betrag>=100000&&this.z100>0)z=this.z100;
	else if(betrag>= 50000&&this.z50 >0)z=this.z50;
	else if(betrag>= 25000&&this.z25 >0)z=this.z25;
	else if(betrag>= 12500&&this.z12 >0)z=this.z12;
	else if(betrag>= 10000&&this.z10 >0)z=this.z10;
	else z=this.z;
	if(z)this.zins=str_replace(".",",",z)+"\u00a0%";
//alert("betrag="+betrag+": z="+z+" ("+this.z+";"+this.z10+";"+this.z12+";"+this.z25+","+this.z50+";"+this.z100+";"+this.z200+"): zins="+this.zins);
	z=str_replace(",",".",z);
	if(z<=0){
		this.info="kein Angebot für diese Laufzeit";
		return 0;
	}
	if(lzm<=0)return "";
	this.info=TesteEinlagensicherung(betrag,this.es,this.info);
	if(this.anzZinstermine<=0)this.anzZinstermine=1;

	var anzZinstermine=this.anzZinstermine;
	if(anzZinstermine<=0)anzZinstermine=1;
	z/=100.0;
	var zinsperioden=anzZinstermine*lzj;
	ganzePerioden=Math.floor(zinsperioden+0.000001);
	end=betrag;
	if(ganzePerioden)
		end*=Math.pow(1.0+z/anzZinstermine,ganzePerioden);
	if(ganzePerioden<zinsperioden-0.000001)
		end*=1.0+z*(lzj-ganzePerioden/(anzZinstermine));
	var geb=this.GetGebuehren(lzm);
	end-=geb;
	end=Math.round(end*100)/100.0;
//alert("BerechneEndbetrag(b="+betrag+","+az="+anzZinstermine+",j="+lzj.","+z+"): "+end+"<BR>";

	return this.endbetrag=end;
}
