// (c) 2008-2009 Werner Zauzig

function GetLayerPositionAbs(id){
	var tobj=document.getElementById(id);
	if(!tobj)return null;
	var abs=GetAbsolutCoord(tobj);
	//alert("y="+abs.y+" scrolly="+scroll.y);
	abs.x-=8;
	return abs;
}

function LayerPosition(id,idmax,doset){
	var obj=document.getElementById(id);
	if(!obj)return;
	var scroll=GetScrollXY();
	var y=scroll.y;
	var abs=GetLayerPositionAbs(id+"Abs");
	if(!abs)return;
	if(abs.y>y)y=abs.y;

	var q=document.getElementById(idmax);
	if(q){
		var qy=GetAbsolutCoord(q).y-150;
		if(qy>y)y=qy;
	}
	if(doset){
		obj.style.top=y+"px";
		obj.style.left=abs.x+"px";
	}
	return new Coord(abs.x,y);
}

function MyWindowOnscroll(e){
	LayerPosition("RechnerLayer","anbieterquelle_1",1);
}

function MoveLayerOnPathFunction(t){
	var p0=GetLayerPositionAbs("RechnerLayerAbs");
	if(!p0)return new Coord(0,0);
	var p1=LayerPosition("RechnerLayer","anbieterquelle_1");
	if(!p1)return null;

	if(p0.y>=p1.y)p0.y=(p1.y<150?p1.y-100:100);
	t=t/10;
	var daempf=Math.log(t+1)+1;
	//daempf*=daempf;
	if(t>8)daempf*=(t*t-64);
	var y=p1.y-Math.abs(Math.cos(t))*(p1.y-p0.y)/daempf;
//alert("Move("+t+") daempf="+daempf+"y="+y);
	if(t>10 && y+5>=p1.y)return null;
	return new Coord(p0.x,y);
}

function MoveLayerOnPath(ID,t){
	var obj=document.getElementById(ID);
	if(!obj){
		setTimeout("MoveLayerOnPath('"+ID+"')",100);//100 msec
		return;
	}
	if(!obj.MoveLayerOnPathFunction)return;
	var c=obj.MoveLayerOnPathFunction(t==null?0:t);
	if(!c){
		LayerPosition("RechnerLayer","anbieterquelle_1",1);
		return;
	}
//alert("MovePath("+ID+","+t+"): "+c);
	if(c.x==0&&c.y==0)return;
	obj.style.left=c.x+"px";
	obj.style.top=c.y+"px";
	setTimeout("MoveLayerOnPath('"+ID+"',"+(t+1)+")",25);//40 msec
}

function GetMouseXY(e){
	if(e.pageX)
		return new Coord(e.pageX,e.pageY);
	if(!window.event || !window.event.clientX) return new Coord(0,0);
	if(document.compatMode && document.compatMode!='BackCompat')//StrictMode
		return new Coord(window.event.clientX+document.documentElement.scrollLeft,window.event.clientY+document.documentElement.scrollTop);
	return new Coord(window.event.clientX+document.body.scrollLeft,window.event.clientY+document.body.scrollTop);
}

function GetScrollXY(){
//var s="GetScrollXY: window.pageXY="+window.pageXOffset+","+window.pageYOffset+" document.body.scroll="+document.body.scrollLeft+","+document.body.scrollTop;if(document.documentElement)s+=" document.documentElement.scroll="+document.documentElement.scrollLeft+","+document.documentElement.scrollTop;alert(s);
	if(typeof(window.pageYOffset)=='number')//Netscape
		return new Coord(window.pageXOffset,window.pageYOffset);
	if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop))//IE6 standards compliant mode
		return new Coord(document.documentElement.scrollLeft,document.documentElement.scrollTop);
	if(document.body&&(document.body.scrollLeft||document.body.scrollTop))//DOM
		return new Coord(document.body.scrollLeft,document.body.scrollTop);
	return new Coord(0,0);//return [X,Y];
}

function Coord(x,y){
	this.x=(x?x:0);
	this.y=(y?y:0);
	this.toString=function(){return "("+this.x+","+this.y+")";}
	this.equals=function(c){return(this.x==c.x&&this.y==c.y);}
}

function Canvas(x,y,w,h){
	this.Coord=Coord;
	this.Coord(x,y);
	this.width=(w?parseInt(w,10):0);
	this.height=(h?parseInt(h,10):0);
	this.equals=function(c){return(this.width==c.width && this.height==c.height);}
}

Canvas.prototype.isCoordInside=function(coord){
	return (coord.x>this.x && (coord.x<this.x+this.width) && coord.y>this.y && (coord.y<this.y+this.height));
}

function GetAbsolutCoord(o){
	var c=new Coord(0,0);
	while(o){
		c.x+=o.offsetLeft;
		c.y+=o.offsetTop;
		o=o.offsetParent;
	}
	return c;
}

function GetCurrentStyle(o,cssPropertyName){
	if(window.getComputedStyle)
		return window.getComputedStyle(o, '').getPropertyValue(cssPropertyName.replace(/([A-Z])/g, "-$1").toLowerCase());
	if(o.currentStyle)
		return o.currentStyle[cssPropertyName];
	return '';
}

function SetElementAttribute(e,text,i){
	var j,ab,attr,kl,wert;
	for(i=i;i<text.length;i++){
		if(text.charAt(i)==" ")continue;
		if(text.charAt(i)==">")
			break;
		if(text.charAt(i)=="/"&&i+1<text.length&&text.charAt(i+1)==">")
			return i+1;
		ab=i;
		for(;i<text.length;i++)
			if(text.charAt(i)=="="||text.charAt(i)==" ")break;
		attr=text.substr(ab,i-ab).toUpperCase();
		if(text.charAt(i)=="=")i++;
		for(;i<text.length;i++)
			if(text.charAt(i)!=" ")break;
		kl=text.charAt(i);
		if(kl=="\""||kl=="'")
			i++;
		else
			kl=" ";
		for(ab=i;i<text.length;i++)
			if(text.charAt(i)==kl)break;
		wert=text.substr(ab,i-ab);
		if(e==null){
			if(attr=="SRC"){
				var pic= new Image();
				pic.src=wert;//preload
			}
		}else if(attr=="SRC")e.src=wert;
		else if(attr=="ALT")e.alt=wert;
		else if(attr=="HREF")e.href=wert;
		else if(attr=="TARGET")e.target=wert;
		else if(attr=="COLOR")e.color=wert;
		else if(attr=="BORDER")e.style.border=wert;
		if(i>=text.length)break;
	}
	return i;
}

function CreateHTML(obj,text,doAppend,nurPreload){
	if(!obj)return;
	if(!doAppend)obj.innerHTML="";
	var str="";
	var nichtnurBild=false;
	var c,ab,tag,e;
	for(var i=0;i<text.length;i++){
		c=text.charAt(i);
		if(c=="<"){
			if(str!=""){
				if(!nurPreload)
					obj.appendChild(document.createTextNode(str));
				nichtnurBild=true;
				str="";
			}
			for(ab=++i;i<text.length;i++){
				c=text.charAt(i);
				if(c<"A"||c>"Z")break;
			}
			tag=text.substr(ab,i-ab);
			if(nurPreload)
				i=SetElementAttribute(null,text,i);
			else{
				if(tag=="IMG"||tag=="BR"){
					e=obj.appendChild(document.createElement(tag));
					i=SetElementAttribute(e,text,i);
				}else if(tag=="A"||tag=="B"||tag=="I"||tag=="FONT"){
					e=obj.appendChild(document.createElement(tag));
					i=SetElementAttribute(e,text,i);
					var j=text.indexOf("</"+tag,i+1);
					var j2=text.indexOf("</"+tag.toLowerCase(),i+1);
					if(j<i||(j2>=i&&j2<j))j=j2;
					if(j<i){//ende-tag fehlt
						CreateHTML(e,text.substr(i+1),0,nurPreload);
						break;
					}
					CreateHTML(e,text.substring(i+1,j),0,nurPreload);
					if((i=text.indexOf(">",j))<j)break;
				}else
					i=SetElementAttribute(null,text,i);
			}
		}else
			str+=c;
	}
	if(nurPreload||str=="")return nichtnurBild;
	obj.appendChild(document.createTextNode(str));
	return true;
}

function CopyHTMLFromObject(o,obj,doAppend){
	if(!doAppend)o.innerHTML="";
	for(var i=0;i<obj.childNodes.length;i++){
		var n=obj.childNodes[i];
		if(n.nodeType==3)//Textknoten
			o.appendChild(n.cloneNode(true));
		else if(n.nodeType==1&&n.tagName!="IMG")//Elementknoten
			o.appendChild(n.cloneNode(true));
	}
}

var RechnerLayerLfdnr=0;
function LogDragDropOperation(id,rid,aid){
	if(id=="Init")return;
	//var logger= new Image();
	//logger.onload=function(){var s=logger.src;}//nur wegen IE ...
	var logger=document.getElementById("etrackerlog");
	//if(logger)logger.src="http://www.biallo.de/finserv/rechner/RechnerLayer/testlogger.php?typ="+rid+"_"+id+"&anbieter="+aid+"&lfdnr="+(++RechnerLayerLfdnr);//logging
	if(logger)logger.src="http://www.biallo.de/includes/trackdrag.php?typ="+rid+"_"+id+"&anbieter="+aid+"&lfdnr="+(++RechnerLayerLfdnr);//logging
}

function DragDropInit(tagQuelle,tagZiel,quelle,ziel,fkt){
	function DragDrop(quelle,ziel,fkt){
		this.source=quelle;
		this.target=ziel;
		this.clientfunction=fkt;
		this.backgroundColor="#ddeeff";
		this.emptyHTML="\u00a0";
		this.targetIds=new Array();
		this.targetIdsMain=new Array();
	}
	DragDrop.prototype.SetTargets=function(ziele){
		for(var i=0;i<ziele.length;i++){
			var o=document.getElementById(ziele[i]);
			var abs=GetAbsolutCoord(o);
			this[ziele[i]]=new Canvas(abs.x,abs.y,o.offsetWidth,o.offsetHeight);
		}
	}
	DragDrop.prototype.HandleOverlapping=function(mCoord){
		var id=this.isOverlapped(mCoord);
		for(var i=0;i<this.targetIds.length;i++)
			if(id!=this.targetIdsMain[i])
				this.SetStyle(this.targetIds[i],"","","");
			else
				this.SetStyle(this.targetIds[i],this.backgroundColor,"none","none");
		//this.SetStyle(id,this.backgroundColor,"inset");
	}
	DragDrop.prototype.isOverlapped=function(mCoord){
		for(var i=0;i<this.targetIds.length;i++){
			if(this[this.targetIds[i]].isCoordInside(mCoord)){
//var id=this.targetIds[i];alert("isOverlapped="+this.targetIdsMain[i]+" ("+id+"=>"+id.substring(id.indexOf(this.target))+")");
				return this.targetIdsMain[i];//error
				var id=this.targetIds[i];
				return id.substring(id.indexOf(this.target));
			}
		}
		return null;
	}
	DragDrop.prototype.SetStyle=function(id,bgcol,bg,border){
		if(id==null||!id)return;
		var o=document.getElementById(id);
		o.style.backgroundColor=bgcol;
		o.style.borderStyle=border;
		//o.style.background=bg;//geht nicht
	}
	thisDragDrop=new DragDrop(quelle,ziel,fkt);
	DragDrop.prototype.DrawObject=function(obj,id,isdrop){
		var o=document.getElementById(id);
		//thisDragDrop.SetStyle(id,"","","");
		for(var i=0;i<thisDragDrop.targetIds.length;i++)
			if(id==thisDragDrop.targetIdsMain[i])
				thisDragDrop.SetStyle(this.targetIds[i],"","","");
		if(thisDragDrop.clientfunction){
			thisDragDrop.clientfunction(o,obj,isdrop);
			return;
		}
		CopyHTMLFromObject(o,obj);
	}
	DragDrop.prototype.CloneObject=function(o){
		var obj=o.cloneNode(true);
		var anza=0;
		for(var i=0;i<obj.childNodes.length;i++){
			var n=obj.childNodes[i];
			if(n.nodeType==1&&n.tagName=="A")
				if(++anza>1)
					obj.removeChild(n);			
		}
		obj.style.width=GetCurrentStyle(o,"width");
		obj.style.height=GetCurrentStyle(o,"height");
		obj.style.position="absolute";
		obj.style.display="none";//invisible
		return obj;
	}
	DragDrop_onmousedown=function(e){
		e=(e||window.event);
		thisDragDrop.SetTargets(thisDragDrop.targetIds);
		var o=(e.target||e.srcElement);
		if(o==null||!o)return;
		//if(o.tagName!="IMG")return;
		if(e.preventDefault)//IE
			e.preventDefault();
		//while(o.tagName!=tagQuelle)o=o.parentNode;
		while(o.tagName!=tagQuelle||o.id.indexOf(thisDragDrop.source)!=0)o=o.parentNode;//if(!(o=o.parentNode))return;

		var oCoord=GetAbsolutCoord(o);
		var mCoord=GetMouseXY(e);
		clickDiffCoord=new Coord(
			Math.abs(oCoord.x-parseInt(mCoord.x,10)-o.parentNode.scrollLeft),
			Math.abs(oCoord.y-parseInt(mCoord.y,10)-o.parentNode.scrollTop)
		);

		if(o.id.indexOf("_link")>0){
			var oq=document.getElementById(str_replace("_link","",o.id));
			if(oq)o=oq;
		}
		thisDragDrop.object=o;
		var obj=thisDragDrop.CloneObject(o);

		document.body.appendChild(obj);
		var OnMouseMoveAlt=document.onmousemove;
		var OnMouseUpAlt=document.onmouseup;
		var OnMouseUpNeu=function(e){// Eventhandler
			document.onmouseup=OnMouseUpAlt;
			document.onmousemove=OnMouseMoveAlt;
			e=(e||window.event);
			var coord=GetMouseXY(e);
			var ziel=thisDragDrop.isOverlapped(coord);
			if(ziel!=null)
				thisDragDrop.DrawObject(obj,ziel,1);
			thisDragDrop.SetTargets(thisDragDrop.targetIds);
			document.body.removeChild(obj);
		}
		document.onmouseup=OnMouseUpNeu;
		document.onmousemove=function(me){// Eventhandler
			if(obj){
				var e=(me||window.event);
				obj.style.display="";
				var coord=GetMouseXY(e);
				obj.style.left=coord.x-clickDiffCoord.x+"px";
				obj.style.top =coord.y-clickDiffCoord.y+"px";
				thisDragDrop.HandleOverlapping(coord);// Mozillla, IE:preventDefault
				//geht nicht auf windows.onscroll
				if(e.clientY<15)window.scrollBy(0,-10);
				else if(document.documentElement && e.clientY>document.documentElement.offsetHeight-15)window.scrollBy(0,10);
				else if(e.clientY>window.innerHeight-15)window.scrollBy(0,10);
				
				if(e.clientX<15)window.scrollBy(-5,0);
				else if(document.documentElement && e.clientX>document.documentElement.offsetWidth-15)window.scrollBy(5,0);
				else if(e.clientX>window.innerWidth-15)window.scrollBy(5,0);
			}
			if(OnMouseMoveAlt!=null)
				OnMouseMoveAlt(me);
		}
		document.ondragstart=function(e){return false;}// Scheiss IE!!!!
	}
	DragDrop.prototype.PreloadTarget=function(zielid,id,tab,index){
		var quellid="";
		for (var i in tab){
			if(id==tab[i][index]){
				quellid=i;
				break;
		}	}
		if(!quellid)return thisDragDrop;
		var o=document.getElementById(quellid);
		if(!o)return thisDragDrop;
		var obj=thisDragDrop.CloneObject(o);
		thisDragDrop.DrawObject(obj,zielid,0);
		return thisDragDrop;
	}

	z=document.getElementsByTagName(tagZiel);
	var id,pos;
	for(var i=0;i<z.length;i++){
		if((pos=(id=z[i].id).indexOf(thisDragDrop.target))>=0){
			thisDragDrop.targetIds.push(id);
			thisDragDrop.targetIdsMain.push(id.substring(pos));
//alert("pos="+pos+" targetIds.push("+id+") targetIdsMain.push("+id.substring(pos)+")");
		}
	}
//for(var i=0;i<this.targetIds.length;i++)alert("targetIds["+i+"]="+this.targetIds[i]+" targetIdsMain["+i+"}="+this.targetIdsMain[i]+")");
	thisDragDrop.SetTargets(thisDragDrop.targetIds);

	//var q=document.getElementsByName(quelle);//nicht IE
	var q=document.getElementsByTagName(tagQuelle);
	for(i=0;i<q.length;i++){
		if((o=q[i]).id.indexOf(thisDragDrop.source)==0 && o.id.indexOf("_link")<0)
			o.onmousedown=DragDrop_onmousedown;
	}

	return thisDragDrop;
}

function str_replace(alt,neu,str){
	var s="";
	if(str==null||(str=str.toString()).length<=0)return s;
	var ialt=0;
	for(var i=str.indexOf(alt);i>=0;i=str.indexOf(alt,i)){
		s+=str.substring(ialt,i)+neu;
		ialt=(i+=alt.length);
	}
	return s+str.substring(ialt,str.length);
}

String.prototype.trim=function(){
	var x=this;
	x=x.replace(/^\s*(.*)/, "$1");
	x=x.replace(/(.*?)\s*$/, "$1");
	return x;
}

function number_check(x,dflt,komma){
//if(x==null){alert("number_check("+x+","+dflt+","+komma+")="+dflt);return dflt;}
	if(x==null)return dflt;
	komma=(komma==null?",":komma);
	var str=x.toString().trim();
	str=str_replace(".","",str);
	var i=0;
	if(i<str.length&&str.charAt(i)=="-")i--;
	for(;i<str.length;i++){
		var c=str.charAt(i);
		if(c==komma)
			str=str.substr(0,i)+"."+str_replace(komma," ",str.substr(i+1));
		else if((c<"0"||c>"9")&&c!=" ")
			str=str.substr(0,i);
	}
	str=str_replace(" ","",str);
	if(str!="")str=1*str;
//alert("number_check("+x+","+dflt+","+komma+")="+str);
	return str;
}

function number_format(x,nachk,komma,punkt){
	if(x==null)return "\u00a0";
	komma=(komma==null?",":komma);
	punkt=(punkt==null?".":punkt);
	if(nachk!=null){
		var zp=Math.pow(10,nachk);
		x=Math.round(x*zp)/zp;
	}
	var v=(x<0?"-":"");
	var xvork=(x>0?Math.floor(x)
			:Math.abs(Math.ceil(x))).toString();
	var xnachk=x.toString().substring(xvork.length+v.length);
	xnachk=(nachk!=null&&nachk>0||xnachk.length>1
			?komma+xnachk.substring(1):"");
	if(nachk!=null && nachk>0)
		for(i=xnachk.length-1,z=nachk; i<z; i++)
			xnachk+="0";
	if(punkt!="")
		for(i=xvork.length-3; i>0; i-=3)
			xvork=xvork.substring(0,i)+punkt+xvork.substring(i);
	return v+xvork+xnachk;
}

function TesteEinlagensicherung(betrag,estxt,info){
	//estxt=str_replace("T",".000",str_replace(",",", ",str_replace(":",": ",str_replace("M"," M",str_replace(" ","",estxt)))));
	estxt=str_replace("T",".000",str_replace(" €","€",str_replace("e ES","",estxt)));
	var es=number_check(estxt,0);
	var estest=estxt.toLowerCase()
	if(estest.indexOf("mio.")>0||estest.indexOf("mill.")>0)
		es*=1000000;
	else if(estest.indexOf("mrd.")>0)
		es*=1000000000;
//alert("estxt="+estxt+ " es="+es+" betrag="+betrag);

	if(es==100||(es>=100000 && betrag*1.2<es))return info;
	estxt="Einlagensicherung "+estxt;
	if(betrag>es)
		estxt="<FONT color=\"#EE0000\">"+estxt+"</FONT>";
	info+=(info?", ":"")+estxt;
	return info;
}

function GetFirstAHref(obj){
	var href;
	if(obj)for(var i=0;i<obj.childNodes.length;i++){
		var n=obj.childNodes[i];
		if(n.nodeType==1){
			if(n.tagName=="A")	//Elementknoten A
				return n.href;
			if(href=GetFirstAHref(n))
				return href;
		}
	}
	return null;
}

function CreateHTMLLinkName(bereich,o,objid,name,defaultlink){
	var href=GetFirstAHref(document.getElementById(objid+"_link"));
	if(href){
		var n=name.toLowerCase();
		if(n.indexOf("d-west-kreditbank")>0)
			name="SWK Bank";
		href=str_replace("source=Rechner-Geldanlage","source="+bereich,href);
		href=str_replace("source=Tabelle-","source=",href);
		href=str_replace("source=iButton-","source=",href);
		href=str_replace("source=Anfragebox-","source=",href);
		href=str_replace("source="+bereich,"source=Schnellvergleich-"+bereich,href);
		CreateHTML(o,"<A HREF=\""+href+"\" TARGET=\"_blank\">"+name+"</A>");
		return;
	}
	CopyHTMLFromObject(o,defaultlink);
}

function CreateHTMLergebnis(tagid,bez,inhalt,sichtbar){
	CreateHTML(document.getElementById(tagid),sichtbar?bez+": "+inhalt:"\u00a0");
}

function CreateHTMLanbieter(typ,o,objid,name,def,produkt){
	//alert(objid+": n="+name+"<BR>p="+produkt);
	CreateHTMLLinkName(typ,o,objid,name,def);
	CreateHTML(o,"<BR>"+produkt+"\u00a0",true);
	o.className="ausgewaehltanbieter";
	//CreateHTML(o,"<BR>"+objid,1);
}
