// (c) 2008-2009 Werner Zauzig

//var svid='fg';

function FestgeldDaten(csv){
	var i=0;
	this.ID=csv[i++];
	i++;this.aArt="Festgeld";//"F"
	this.name=str_replace("&"," & ",csv[i++]);

	this.za1=number_check(csv[i++],0);
	this.za3=number_check(csv[i++],0);
	this.za6=number_check(csv[i++],0);
	this.za12=number_check(csv[i++],0);
	this.zb1=number_check(csv[i++],0);
	this.zb3=number_check(csv[i++],0);
	this.zb6=number_check(csv[i++],0);
	this.zb12=number_check(csv[i++],0);
	this.zc1=number_check(csv[i++],0);
	this.zc3=number_check(csv[i++],0);
	this.zc6=number_check(csv[i++],0);
	this.zc12=number_check(csv[i++],0);

	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++];

	this.zf1=csv[i++].trim();
	this.zf3=csv[i++].trim();
	this.zf6=csv[i++].trim();
	this.zf12=csv[i++].trim();

	//var anbtyp=csv[i++];
	//this.fuerAlle=(anbtyp.indexOf("0")>0?0:1);
	if(!this.einmGeb)this.einmGeb=0;
	this.mtlGeb=0;
	this.info="";
}

FestgeldDaten.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);
}

FestgeldDaten.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;
}

FestgeldDaten.prototype.SetErgebnis=function(betrag,lzm){
	betrag=1*betrag;
	if(betrag<=0)betrag=1;
	lzm=1*lzm;
	this.zins=this.endbetrag="\u00a0";
	if(this.fuer!=""){
		var s=str_replace("Privatpersonen","",str_replace("nur","",this.fuer)).trim();
//alert("this.info.charAt(1)="+this.info.charAt(1)+" this.info.substr(2)="+this.info.substr(2));
		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, mind=this.mind;
	var anzZinstermine;
	if(lzm>=24){z=0;
       	}else if (lzm>=12){
		anzZinstermine=1;
		if(this.zf12!="")
			z=100*this.GetFormelErgebnis(this.zf12,betrag,lzm,1)/betrag;
		else if	(betrag>=50000&&this.zc12>0){	z=this.zc12;mind=50000;
		}else if(betrag>=25000&&this.zb12>0){	z=this.zb12;mind=25000;
		}else{					z=this.za12;mind=5000;
		}
       	}else if (lzm>= 6){
		anzZinstermine=2;
		if(this.zf6!="")
			z=100*this.GetFormelErgebnis(this.zf6,betrag,lzm,1)/betrag;
		else if	(betrag>=50000&&this.zc6>0){	z=this.zc6;mind=50000;
		}else if(betrag>=25000&&this.zb6>0){	z=this.zb6;mind=25000;
		}else{					z=this.za6;mind=5000;
		}
       	}else if (lzm>= 3){
		anzZinstermine=4;
		if(this.zf3!="")
			z=100*this.GetFormelErgebnis(this.zf3,betrag,lzm,1)/betrag;
		else if	(betrag>=50000&&this.zc3>0){	z=this.zc3;mind=50000;
		}else if(betrag>=25000&&this.zc3>0){	z=this.zb3;mind=25000;
		}else{					z=this.za3;mind=5000;
		}
       	}else if (lzm>= 1){
		anzZinstermine=12;
		if(this.zf1!="")
			z=100*this.GetFormelErgebnis(this.zf1,betrag,lzm,1)/betrag;
		else if	(betrag>=50000&&this.zc1>0){	z=this.zc1;mind=50000;
		}else if(betrag>=25000&&this.zb1>0){	z=this.zb1;mind=25000;
		}else{					z=this.za1;mind=5000;
		}
	}else{z=0;}
	//if(!this.mind)this.mind=mind;
	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(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;
}

