//-------------------------------------------//
// (c) Arndt Brünner, Gelnhausen, 23.1.2004  //
//     Alle Rechte vorbehalten!              //
//-------------------------------------------//
//                        Version: 9.4.2010

var rd=100000000,tf=Math.PI/180;
function berechne()
{
	var F=document.f;
	var z=0,ok=(1==1),i,v=new Array("a","b","c","p","q","h","f","u","wa","wb");
	var a,b,c,p,q,h,f,u,wa,wb,ag,bg,cg,pg,qg,hg,fg,ug,wag,wbg,e="",E="";
	for(i=0;i<v.length;i++)
	{
		eval("F."+v[i]+"_.value='';");
		eval(v[i]+"=kp(F."+v[i]+".value);");
		eval(v[i]+"g=("+v[i]+">0)&&(!isNaN("+v[i]+"));if("+v[i]+"g)z++;");
	}

	if(z>2){ok=false;e+="Mehr als zwei Eingaben sind hier unzulässig\n";}
	if(wag&&wbg){ok=false;e+="Fehler: Beide Winkel sind gegeben\n";}
	if(z<2)ok=false;

	while(ok){
	ok=false;
	//alert("ag: "+ag+"\nbg: "+bg+"\ncg: "+cg+"\npg: "+pg+"\nqg: "+qg+"\nhg: "+hg+"\nfg: "+fg+"\nug: "+ug+"\nwag: "+wag+"\nwbg: "+wbg);
	if(ag&&bg&&!cg){c=Math.sqrt(a*a+b*b);E+="c=sqr(a²+b²)\n";ok=cg=true;continue;}
	if(ag&&!bg&&cg){if(a>=c){e+="a >= c\n";break;}b=Math.sqrt(c*c-a*a);E+="b=sqr(c²-a²)\n";ok=bg=true;continue;}
	if(!ag&&bg&&cg){if(b>=c){e+="b >= c\n";break;}a=Math.sqrt(c*c-b*b);E+="a=sqr(c²-b²)\n";ok=ag=true;continue;}

	if(ug&&ag&&bg&&!cg){if(u<=a+b){e+="a+b >= u\n";break;}c=u-a-b;E+="c=u-a-b\n";ok=cg=true;continue;}
	if(ug&&ag&&!bg&&cg){if(u<=a+c){e+="a+c >= u\n";break;}b=u-a-c;E+="b=u-a-c\n";ok=bg=true;continue;}
	if(ug&&!ag&&bg&&cg){if(u<=a+b){e+="b+c >= u\n";break;}a=u-c-b;E+="a=u-b-c\n";ok=ag=true;continue;}
	if(!ug&&ag&&bg&&cg){u=a+b+c;E+="u=a+b+c\n";ok=ug=true;continue;}

	if(wag&&!wbg){if(wa>=90){e+="alpha >= 90°\n";break;}wb=90-wa;E+="beta=90°-alpha\n";ok=wbg=true;continue;}
	if(!wag&&wbg){if(wb>=90){e+="beta >= 90°\n";break;}wa=90-wb;E+="alpha=90°-beta\n";ok=wag=true;continue;}

	if(hg&&pg&&!qg){q=h*h/p;E+="q=h²/p\n";ok=qg=true;continue;}
	if(hg&&!pg&&qg){p=h*h/q;E+="p=h²/q\n";ok=pg=true;continue;}
	if(!hg&&pg&&qg){h=Math.sqrt(p*q);E+="h=sqr(p*q)\n";ok=hg=true;continue;}

	if(pg&&qg&&!cg){c=p+q;E+="c=p+q\n";ok=cg=true;continue;}
	if(pg&&!qg&&cg){if(p>=c){e+="p >= c\n";break;}q=c-p;E+="q=c-p\n";ok=qg=true;continue;}
	if(!pg&&qg&&cg){if(q>=c){e+="q >= c\n";break;}p=c-q;E+="p=c-q\n";ok=pg=true;continue;}

	if(ag&&qg&&!cg){c=a*a/q;E+="c=a²/q\n";ok=cg=true;continue;}
	if(ag&&!qg&&cg){q=a*a/c;E+="q=a²/c\n";ok=qg=true;continue;}
	if(!ag&&qg&&cg){a=Math.sqrt(c*q);E+="a=sqr(c*q)\n";ok=ag=true;continue;}

	if(bg&&pg&&!cg){c=b*b/p;E+="c=b²/p\n";ok=cg=true;continue;}
	if(bg&&!pg&&cg){p=b*b/c;E+="p=b²/c\n";ok=pg=true;continue;}
	if(!bg&&pg&&cg){b=Math.sqrt(c*p);E+="b=sqr(c*p)\n";ok=bg=true;continue;}

	if(fg&&hg&&!cg){c=2*f/h;E+="c=2A/h\n";ok=cg=true;continue;}
	if(fg&&!hg&&cg){h=2*f/c;E+="h=2A/c\n";ok=hg=true;continue;}
	if(!fg&&hg&&cg){f=c*h/2;E+="A=c*h/2\n";ok=fg=true;continue;}

	if(fg&&ag&&!bg){b=2*f/a;E+="b=2A/a\n";ok=bg=true;continue;}
	if(fg&&!ag&&bg){a=2*f/b;E+="a=2A/b\n";ok=ag=true;continue;}
	if(!fg&&ag&&bg){f=b*a/2;E+="A=a*b/2\n";ok=fg=true;continue;}

	if(ag&&hg&&!qg){if(h>=a){e+="h >= a\n";break;}q=Math.sqrt(a*a-h*h);E+="q=sqr(a²-h²)\n";qg=ok=true;continue;}
	if(bg&&hg&&!pg){if(h>=b){e+="h >= b\n";break;}p=Math.sqrt(b*b-h*h);E+="p=sqr(b²-h²)\n";pg=ok=true;continue;}

	if(wag&&hg&&!pg){p=h/Math.tan(wa*tf);E+="p=h/tan(alpha)\n";ok=pg=true;continue;}
	if(wbg&&hg&&!qg){q=h/Math.tan(wb*tf);E+="q=h/tan(beta)\n";ok=qg=true;continue;}

	if(wag&&!hg&&pg){h=p*Math.tan(wa*tf);E+="h=p*tan(alpha)\n";ok=hg=true;continue;}
	if(wbg&&!hg&&qg){h=q*Math.tan(wb*tf);E+="h=q*tan(beta)\n";ok=hg=true;continue;}

	if(!wag&&hg&&pg){wa=Math.atan(h/p)/tf;E+="alpha=atan(h/p)\n";ok=wag=true;continue;}
	if(!wbg&&hg&&qg){wb=Math.atan(h/q)/tf;E+="beta=atan(h/q)\n";ok=wbg=true;continue;}

	if(hg&&fg&&!pg&&!qg)
	{
		if(f<h*h){e+="A < h²\n";break;}
		var w=Math.sqrt(f*f-h*h*h*h);p=(w+f)/h;q=(f-w)/h;
		e+="(a|q|alpha) und (b|p|beta) können vertauscht werden\n";
		E+="p=(A+x)/h  und  q=(A-x)/h\n  mit x=sqr(A²-h^4)\n(a|q|alpha) und (b|p|beta) können vertauscht werden\n";
		pg=qg=ok=true;continue;
	}

	if(wag&&ag&&!bg){b=a/Math.tan(wa*tf);E+="b=a/tan(alpha)\n";bg=ok=true;continue;}
	if(wbg&&ag&&!bg){b=a*Math.tan(wb*tf);E+="b=a*tan(beta)\n";bg=ok=true;continue;}
	if(wag&&!ag&&bg){a=b*Math.tan(wa*tf);E+="a=b*tan(alpha)\n";ag=ok=true;continue;}
	if(wbg&&!ag&&bg){a=b/Math.tan(wb*tf);E+="a=b/tan(beta)\n";ag=ok=true;continue;}

	if(!wbg&&cg&&ag){wb=Math.acos(a/c)/tf;E+="beta=acos(a/c)\n";wbg=ok=true;continue;}
	if(!wag&&cg&&bg){wa=Math.acos(b/c)/tf;E+="alpha=acos(b/c)\n";wag=ok=true;continue;}

	if(wbg&&!cg&&ag){c=a/Math.cos(wb*tf);E+="c=a/cos(beta)\n";cg=ok=true;continue;}
	if(wag&&!cg&&bg){c=b/Math.cos(wa*tf);E+="c=b/cos(alpha)\n";cg=ok=true;continue;}

	if(wbg&&cg&&!ag){a=c*Math.cos(wb*tf);E+="a=c*cos(beta)\n";ag=ok=true;continue;}
	if(wag&&cg&&!bg){b=c*Math.cos(wa*tf);E+="b=c*cos(alpha)\n";bg=ok=true;continue;}

	if(ug&&fg&&!cg){c=(u*u-4*f)/(2*u);E+="c=(u²-4A)/(2u)\n";cg=ok=true;continue;}
	if(ug&&!fg&&cg){f=u*(u-2*c)/4;E+="A=u(u-2c)/4\n";fg=ok=true;continue;}
	if(!ug&&fg&&cg){u=Math.sqrt(c*c+4*f)+c;E+="u=sqr(c²+4f)\n";ug=ok=true;continue;}

	if(wag&&fg&&!ag)
	{
		var sina=Math.sin(wa*tf);
		a=Math.sqrt(2)*Math.sqrt(f)*Math.sqrt(sina)/Math.pow(1-sina*sina,1/4);
		b=Math.sqrt(2)*Math.sqrt(f)*Math.pow(1-sina*sina,1/4)/Math.sqrt(sina);
		E+="a=sqr(2)*sqr(A)*sqr(sin(alpha))/sqr(sqr(1-(sin(alpha))²))\n";
		E+="b=sqr(2)*sqr(A)*sqr(sqr(1-(sin(alpha))²))/sqr(sin(alpha))\n";
		//E+=" [a|q|alpha] und [b|p|beta] können vertauscht werden\n";
		//e+="[a|q|alpha] und [b|p|beta] können vertauscht werden\n";
		ag=bg=ok=true;continue;
	}

	if(fg&&ug&&!ag)
	{
		a=(Math.sqrt(16*f*f-24*f*u*u+u*u*u*u)+4*f+u*u)/(4*u);
		b=-(Math.sqrt(16*f*f-24*f*u*u+u*u*u*u)-4*f-u*u)/(4*u);
		E+="a=(4A+u²+x)/(4u)\nb=(4A+u²-x)/(4u)\n mit x=sqr(16A²-24Au²+u^4)\n";
		E+=" [a|q|alpha] und [b|p|beta] können vertauscht werden\n";
		e+="[a|q|alpha] und [b|p|beta] können vertauscht werden\n";
		ag=bg=ok=true;continue;
	}

	if(pg&&fg&&!qg)
	{
		var p4=p*p*p*p,f2=f*f;
		q=(Math.pow(-6*Math.sqrt(3)*f*Math.sqrt(27*f2+p4)+54*f2+p4,1/3)+Math.pow(6*Math.sqrt(3)*f*Math.sqrt(27*f2+p4)+54*f2+p4,1/3)-2*Math.pow(p,4/3))/(3*Math.pow(p,1/3));
		E+="q=((-6*sqr(3)*A*sqrt(27*A²+p^4)+54*A²+p^4)^(1/3)+(6*sqr(3)*A*sqr(27*A²+p^4)+54*A²+p^4)^(1/3)-2*p^(4/3))/(3*p^(1/3))\n";
		qg=ok=true;continue;
	}
	if(!pg&&fg&&qg)
	{
		var q4=q*q*q*q,f2=f*f;
		p=(Math.pow(-6*Math.sqrt(3)*f*Math.sqrt(27*f2+q4)+54*f2+q4,1/3)+Math.pow(6*Math.sqrt(3)*f*Math.sqrt(27*f2+q4)+54*f2+q4,1/3)-2*Math.pow(q,4/3))/(3*Math.pow(q,1/3));
		E+="p=((-6*sqr(3)*A*sqrt(27*A²+q^4)+54*A²+q^4)^(1/3)+(6*sqr(3)*A*sqr(27*A²+q^4)+54*A²+q^4)^(1/3)-2*q^(4/3))/(3*q^(1/3))\n";
		pg=ok=true;continue;
	}

	if(pg&&ug&&!ag)
	{
		var x0=(4*p*p*p+6*p*p*u+21*p*u*u-4*u*u*u)/(4*Math.pow(p*p+p*u+u*u,3/2));
		var x1=Math.acos(-x0)/3;
		var x2=Math.asin(x0)/3;
		var t="x=(4p³+6p²u+21pu²-4u³)/(4(p²+pu+u²)^(3/2))\n";
		var t2="      y=acos(-x)/3\n",t3="      z=asin(x)/3\n";
		var pi=Math.PI;
		a1=-4*(p*p+p*u+u*u)*Math.pow(Math.sin(x2),2)/(9*p)-2*(p+2*u)*Math.sqrt(p*p+p*u+u*u)*Math.sin(x2)/(9*p)+(2*p*p+8*p*u-u*u)/(9*p);
		b1=2*Math.sqrt(p*p+p*u+u*u)*Math.sin(x2)/3-(p-u)/3;
		var E1="a=-4(p²+pu+u²)*sin²(z)/(9p)-2(p+2u)*sqr(p²+pu+u²)*sin(z)/(9p)+(2p²+8pu-u²)/(9p)\n";
		E1+="b=2(sqr(p²+pu+u²)*sin(z)-p+u)/3\n mit: "+t+t3;
		//c=4*(p*p+p*u+u*u)*Math.sin(x2)^2/(9*p)+4*Math.sqrt(p*p+p*u+u*u)*(u-p)*Math.sin(x2)/(9*p)+(p-u)^2/(9*p);
		//q=4*(p*p+p*u+u*u)*Math.sin(x2)^2/(9*p)+4*Math.sqrt(p*p+p*u+u*u)*(u-p)*Math.sin(x2)/(9*p)-(8*p*p+2*p*u-u*u)/(9*p);
		//a=-4*(p*p+p*u+u*u)*Math.pow(Math.sin(x2+pi/3),2)/(9*p)+2*(p+2*u)*Math.sqrt(p*p+p*u+u*u)*Math.sin(x2+pi/3)/(9*p)+(2*p*p+8*p*u-u*u)/(9*p);
		//b2=-2*Math.sqrt(p*p+p*u+u*u)*Math.sin(x2+pi/3)/3-(p-u)/3;
		//c=4*(p*p+p*u+u*u)*Math.sin(x2+pi/3)^2/(9*p)+4*(p-u)*Math.sqrt(p*p+p*u+u*u)*Math.sin(x2+pi/3)/(9*p)+(p-u)^2/(9*p);
		//q=4*(p*p+p*u+u*u)*Math.sin(x2+pi/3)^2/(9*p)+4*(p-u)*Math.sqrt(p*p+p*u+u*u)*Math.sin(x2+pi/3)/(9*p)-(8*p*p+2*p*u-u*u)/(9*p);
		a2=-4*(p*p+p*u+u*u)*Math.pow(Math.cos(x1),2)/(9*p)-2*(p+2*u)*Math.sqrt(p*p+p*u+u*u)*Math.cos(x1)/(9*p)+(2*p*p+8*p*u-u*u)/(9*p);
		b2=2*Math.sqrt(p*p+p*u+u*u)*Math.cos(x1)/3-(p-u)/3;
		var E2="a=-4(p²+pu+u²)*cos²(y)/(9p)-2(p+2u)*sqr(p²+pu+u²)*cos(y)/(9p)+(2p²+8pu-u²)/(9p)\n";
		E2+="b=2(sqr(p²+pu+u²)*cos(y)-p+u)/3\n mit: "+t+t2;
		//c=4*(p*p+p*u+u*u)*Math.cos(x1)^2/(9*p)+4*Math.sqrt(p*p+p*u+u*u)*(u-p)*Math.cos(x1)/(9*p)+(p-u)^2/(9*p);
		//q=4*(p*p+p*u+u*u)*Math.cos(x1)^2/(9*p)+4*Math.sqrt(p*p+p*u+u*u)*(u-p)*Math.cos(x1)/(9*p)-(8*p*p+2*p*u-u*u)/(9*p);
		t="Es gibt eine weitere Lösung mit\n";
		if((a1>0)&&(b1>0)&&(a2>0)&&(b2>0)){e+=t+"a="+r(a2)+" und b="+r(b2)+"\n";E+=E1+" "+t+"\n"+E2;}
		if((a1>0)&&(b1>0)){a=a1;b=b1;ag=bg=ok=true;E+=E1;continue;}
		if((a2>0)&&(b2>0)){a=a2;b=b2;ag=bg=ok=true;E+=E2;continue;}
		break;
	}
	if(qg&&ug&&!bg)
	{
		var x0=(4*q*q*q+6*q*q*u+21*q*u*u-4*u*u*u)/(4*Math.pow(q*q+q*u+u*u,3/2));
		var x1=Math.acos(-x0)/3;
		var x2=Math.asin(x0)/3;
		var t="x=(4q³+6q²u+21qu²-4u³)/(4(q²+qu+u²)^(3/2))\n";
		var t2="      y=acos(-x)/3\n",t3="      z=asin(x)/3\n";
		var pi=Math.PI;
		b1=-4*(q*q+q*u+u*u)*Math.pow(Math.sin(x2),2)/(9*q)-2*(q+2*u)*Math.sqrt(q*q+q*u+u*u)*Math.sin(x2)/(9*q)+(2*q*q+8*q*u-u*u)/(9*q);
		a1=2*Math.sqrt(q*q+q*u+u*u)*Math.sin(x2)/3-(q-u)/3;
		var E1="a=-4(q²+qu+u²)*sin²(z)/(9q)-2(q+2u)*sqr(q²+qu+u²)*sin(z)/(9q)+(2q²+8qu-u²)/(9q)\n";
		E1+="b=2(sqr(q²+qu+u²)*sin(z)-q+u)/3\n mit: "+t+t3;
		b2=-4*(q*q+q*u+u*u)*Math.pow(Math.cos(x1),2)/(9*q)-2*(q+2*u)*Math.sqrt(q*q+q*u+u*u)*Math.cos(x1)/(9*q)+(2*q*q+8*q*u-u*u)/(9*q);
		a2=2*Math.sqrt(q*q+q*u+u*u)*Math.cos(x1)/3-(q-u)/3;
		var E2="a=-4(q²+qu+u²)*cos²(y)/(9q)-2(q+2u)*sqr(q²+qu+u²)*cos(y)/(9q)+(2q²+8qu-u²)/(9q)\n";
		E2+="b=2(sqr(q²+qu+u²)*cos(y)-q+u)/3\n mit: "+t+t2;
		t="Es gibt eine weitere Lösung mit\n";
		if((a1>0)&&(b1>0)&&(a2>0)&&(b2>0)){e+=t+"a="+r(a2)+" und b="+r(b2)+"\n";E+=E1+" "+t+"\n"+E2;}
		if((a1>0)&&(b1>0)){a=a1;b=b1;ag=bg=ok=true;E+=E1;continue;}
		if((a2>0)&&(b2>0)){a=a2;b=b2;ag=bg=ok=true;E+=E2;continue;}
		break;
	}

	if(ag&&pg&&!cg&&!qg)
	{
		var x=Math.sqrt(4*a*a+p*p);
		//b = Math.sqrt(2*p*(x+p))/2;
		c = (x+p)/2;
		q = (x-p)/2;
		E+="c=(x+p)/2\nq=(x-p)/2\n mit: x=sqr(4a²+p²)\n";
		cg=qg=ok=true;continue;
	}
	if(!cg&&!pg&&bg&&qg)
	{
		var x=Math.sqrt(4*b*b+q*q);
		c = (x+q)/2;
		p = (x-q)/2;
		//a = Math.sqrt(c*q);
		E+="c=(x+q)/2\np=(x-q)/2\n mit: x=sqr(4b²+q²)\n";
		cg=pg=ok=true;continue;
	}

	if(ag&&ug&&!bg)
	{
		b=u*u/(2*(a-u))+u;
		//c=(2*a*a-2*a*u+u*u)/(2*(u-a));
		//p=u*u*(2*a-u)*(2*a-u)/(2*(u-a)*(2*a*a-2*a*u+u*u));
		//q=c-p;
		E+="b=u²/(2(a-u))+u\n";//c=(2a²-2au+u²)/(2(u-a))\n*\p=u²*(2a-u)(2a-u)/(2(u-a)(2a²-2au+u²))*/\nq=c-p\n";
		bg=ok=true;continue;
	}
	if(!ag&&ug&&bg)
	{
		a=u*u/(2*(b-u))+u;
		//c=(2*b*b-2*b*u+u*u)/(2*(u-b));
		//q=u*u*(2*b-u)*(2*b-u)/(2*(u-b)*(2*b*b-2*b*u+u*u));
		//p=c-q;
		E+="a=u²/(2(b-u))+u\n";//c=(2b²-2bu+u²)/(2(u-b))\nq=u²*(2b-u)(2b-u)/(2(u-b)(2b²-2bu+u²))\np=c-q\n";
		ag=ok=true;continue;
	}

	if(hg&&ug&&!cg)
	{
		c=u*u/(2*(h+u));
		E+="c=u²/(2(h+u))\n";
		cg=ok=true;continue;
	}

	if(wag&&ug&&!ag)
	{
		var sina=Math.sin(wa*tf);
		a = u*(Math.sqrt(sina + 1) - Math.sqrt(1 - sina))/(2*Math.sqrt(sina + 1));
		E+="a=u*(sqr(sin(alpha)+1)-sqr(1-sin(alpha)))/(2*sqr(sin(alpha)+1))\n";
		ag=ok=true;continue;
	}

	}

	//if(e!="")alert(e);

	/*if(Math.abs(p+q-c)>1e-10)e+="p+q=c geht nicht auf\n";
	if(Math.abs(a*a+b*b-c*c)>1e-10)e+="a²+b²=c² geht nicht auf\n";
	if(Math.abs(p*q-h*h)>1e-10)e+="p·q=h² geht nicht auf\n";
	if(Math.abs(a*a-q*c)>1e-12)e+="a²=q·c geht nicht auf\n";
	if(Math.abs(b*b-p*c)>1e-12)e+="b²=p·c geht nicht auf\n";
	if(Math.abs(2*f-h*c)>1e-12)e+="A=h·c/2 geht nicht auf\n";
	if(Math.abs(2*f-a*b)>1e-12)e+="a²=a·b/2 geht nicht auf\n";*/


	for(i=0;i<v.length;i++)eval("if("+v[i]+"g&&("+v[i]+">0))F."+v[i]+"_.value=r("+v[i]+");");
	if(F.wa_.value!="")F.wa_.value+="°";
	if(F.wb_.value!="")F.wb_.value+="°";
	E=E.replace(/=/g," = ").replace(/\+/g," + ").replace(/-/g," - ").replace(/\*/g,"·").replace(/\( - /g,"(-");
	E=E.replace(/=  - /g,"= -");//.replace(/ +/g," ");
	if(E.indexOf("sqr")>-1)E+="\n sqr(x) ist die Quadratwurzel von x\n";
	F.e.value=e;
	F.E.value=E;
	if(document.drd647d347d4g!=null)
	if(!isNaN(h)&&!isNaN(p)&&!isNaN(q)&&(h*p*q!=0)&&(Math.abs(p*q-h*h)<1e-5))document.drd647d347d4g.zeichne(p,q);else document.drd647d347d4g.zeichne(-1,0);
}
function kp(t){if(t.replace(/\D/,"")=="")return null;return Number(t.replace(/,/,".").replace(/°/,""));}
function r(x){return (" "+String(Math.round(x*rd)/rd)).replace(/\./,",").replace(/ ,/,"0,").replace(/-,/,"-0,").replace(/ /,"");}
