// geradeablesen.js
// (c) Andreas Meier
// Sophie-Scholl-Realschule Weiden
// erstellt am 26.06. 2005
// geändert am 30.06. 2005
// alle Rechte liegen beim Autor

var m = 0;
var t = 0;
var hilfe = 0;
var fehler = 0;
var gan = 0;
var aufgabe = 0;
var geloest= 0;
var loesung= 0;
var loes= 0;
var score = 0;
var ywert = 0;
var yw = 0;
var ungerade = 0;


function zufall()
{
// Ermittelt eine Zufallszahl
   var zufallszahl = 0;
 zufallszahl=Math.floor(6*Math.random()+1);  // zwischen 1 und 6
return zufallszahl;
}

function zufall2()
{
// Ermittelt eine Zufallszahl
   var zufallszahl2 = 0;
 zufallszahl2=Math.floor(4*Math.random()+1);  // zwischen 1 und 4
return zufallszahl2;
}


function zufall3()
{
// Ermittelt eine Zufallszahl
   var zufallszahl3 = 0;
 zufallszahl3=Math.floor(4*Math.random()+1);  // zwischen 1 und 4
return zufallszahl3;
}

function zufall4()
{
// Ermittelt eine Zufallszahl
   var zufallszahl4 = 0;
 zufallszahl4=Math.floor(5*Math.random()+1);  // zwischen 1 und 5
return zufallszahl4;
}

function Geradezeichnen()
{
applet = document.applets[0];
applet.setVisible("g", false);
applet.evalCommand("A = (0,"+t+")");
applet.evalCommand("B = (1,"+(t+m)+")");
applet.setVisible("g", true);}


function aufgabeneu()
{
document.Para.geradeein.value = "";
hilfe = 0;
geloest = 0;
aufgabe++;
m = zufall();

vorzm = zufall3();
if (vorzm <3) m = (-1)*m;
t = zufall2();
vorzt = zufall3();
if (vorzt <3) t = (-1)*t;

teiler = zufall4();
if (teiler == 3 || teiler >5) teiler = 1;
m = m/teiler;

loes = 1000;
yw= m*1000+t;

document.Para.gleichung.value ="";

if (m < 0) mstring = "- "+String(Math.abs(m));
else mstring = String(m);
mstring = mstring.replace(".",",");

if (m == 1) mstring = "";
if (m == -1) mstring = "- ";
if (t < 0) tstring = " - "+String(Math.abs(t));
else tstring = " + "+String(t);

gleichungstring = " y = "+mstring+"x"+tstring;

Geradezeichnen();
document.Para.geradeein.focus();
}

function zeilelesen(eingabezeile)

{
zae2 = String(eingabezeile).replace(/,/g,".").replace(/ /g,"").replace(/²/g,"^2");

do{
i=zae2.search(/\dx/);
if(i==-1)break;
zae2=zae2.substr(0,i+1)+"*"+zae2.substring(i+1,zae2.length);}
while(true);

do{
i=zae2.search(/\d\(/);
if(i==-1)break;
zae2=zae2.substr(0,i+1)+"*"+zae2.substring(i+1,zae2.length);}
while(true);

do{
i=zae2.search(/x\d/);
if(i==-1)break;
zae2=zae2.substr(0,i+1)+"*"+zae2.substring(i+1,zae2.length);}
while(true);

do{
i=zae2.search(/\)\d/);
if(i==-1)break;
zae2=zae2.substr(0,i+1)+"*"+zae2.substring(i+1,zae2.length);}
while(true);

do{
i=zae2.search(/\)\(/);
if(i==-1)break;
zae2=zae2.substr(0,i+1)+"*"+zae2.substring(i+1,zae2.length);}
while(true);

do{
i=zae2.search(/x\(/);
if(i==-1)break;
zae2=zae2.substr(0,i+1)+"*"+zae2.substring(i+1,zae2.length);}
while(true);

}


function pruefe() {

if (document.Para.geradeein.value != "")
{
zeilelesen(document.Para.geradeein.value);

do
{i=zae2.search("x");
if(i==-1)break;
zae2 = zae2.replace("x",loes)
}
while(true);

ywert = eval(zae2);

if (yw == ywert)
{
if (geloest ==0){

text1 = "Ausgezeichnet!\n";
text2 ="Die Gleichung ist richtig!";
text3 ="";
geloest = 1;
if (hilfe == 0) score = score + 15;
else text3 ="\n Du hast dir aber g zeichnen lassen.";
}
else
{
text1 = "Willst du mich betrügen?\n";
text2 ="Die Aufgabe wurde bereits gelöst!\n";
text3 = "";
score = score - 5;
}

}

else {

text1 = "Leider falsch!\n";
text2 = "Die Gleichung lautet:\n";
text3 = gleichungstring;
score = score - 7;
geloest = 1;
if (score <0) score = 0;
}

text = text1+ text2 + text3;
document.Para.gleichung.value = text;
ausgabe = document.Para.gleichung.value;
ausgabe = ausgabe +"\n Punktestand: "+score+ " bei "+aufgabe+" Aufgaben";
document.Para.gleichung.value = ausgabe;
}
}


function auswerten() {

var punkte = score;
if (punkte < 1) punkte = 1;
// Zurücksetzen der Punkte auf Ausgangswert

m = 0;
t = 0;
hilfe = 0;
fehler = 0;
aufgabe = 0;
geloest= 0;
loesung= 0;
score = 0;

document.Para.gleichung.value = "Deine Punkte werden übertragen.\n Du beginnst wieder bei 0 Punkten.";

/* Achtung muss für jede Datei manuell angepasst werden */
/*                     Anfang                           */
var aufgabenart="Geradengleichung ablesen";
var datnam= "highsqlpost8ww.php";
uebertrag = "<form name = 'eingabe' action ='../../../Mathematik/"+datnam+"' method ='post'>";
auswertung(aufgabenart,punkte,uebertrag);
/*                     Ende                           */
}


