var isFr = false;
if (document.URL.indexOf('_eng')<0) {isFr = true;}

function ligne(x) 
{
//cocher dans une seule ligne
	if (document.f.txtL1.value != x && document.f.txtL1.value != "")
	{
	var unelignetext = "Check as many buttons as you wish\nin a SINGLE line! (A B C D)\nYou must start again!";
		if (isFr) {unelignetext ="Cochez autant de boutons que vous voulez\ndans UNE ligne. (A B C D)\nLa partie va recommencer.";}
	alert(unelignetext);
	uneAutre();
	}
document.f.txtL1.value = x;

//affiche le nombre de boutons restants dans chaque ligne
if (x == 'A')
{document.f.txtA.value = eval(document.f.txtA.value-1);}

if (x == 'B')
{document.f.txtB.value = eval(document.f.txtB.value-1);}

if (x=='C')
{document.f.txtC.value = eval(document.f.txtC.value-1);}

if (x == 'D')
{document.f.txtD.value = eval(document.f.txtD.value-1);}

document.f.txt.value=" ";
}


function uneAutre(form)
{
var my_url = "marienbad_eng.php";
if (isFr) {my_url = "marienbad.php";}

parent.location.href = my_url;
	for (var res = 0; res < document.forms.length; res++)
	{
	document.forms[res].reset();
	}
}



function valide()
{
document.f.txtL2.value = "";
document.f.txt.value = "";
var a = document.f.txtA.value;
var b = document.f.txtB.value;
var c = document.f.txtC.value;
var d = document.f.txtD.value;
var abcd = new Array(a,b,c,d);

// si le joueur passe son tour
	if (document.f.txtAa.value == a && document.f.txtBa.value == b && document.f.txtCa.value == c && document.f.txtDa.value == d)
	{
	var passtext = "You must check at least one button\nYou must start again!";
		if (isFr) {passtext = "Cochez au moins un bouton avant de valider.\nLa partie va recommencer.";}
	alert(passtext);
	uneAutre()
	return false;
	//====
	}
document.f.txtL1.value = "";
document.f.txtAa.value = a;
document.f.txtBa.value = b;
document.f.txtCa.value = c;
document.f.txtDa.value = d;

function analyse() //interne a valide()
{
var oui = new Array("0,0,0,1", "0,0,1,0", "0,0,2,2", "0,0,3,3", "0,0,4,4", "0,0,5,5", "0,1,0,0", "0,1,1,1", "0,1,2,3", "0,1,3,2", "0,1,4,5", "0,1,5,4", "0,2,0,2", "0,2,1,3", "0,2,2,0", "0,2,3,1", "0,2,4,6", "0,2,5,7", "0,3,0,3", "0,3,1,2", "0,3,2,1", "0,3,3,0", "0,3,4,7", "0,3,5,6", "1,0,0,0", "1,0,1,1", "1,0,2,3", "1,0,3,2", "1,0,4,5", "1,0,5,4", "1,1,0,1", "1,1,1,0", "1,1,2,2", "1,1,3,3", "1,1,4,4", "1,1,5,5", "1,2,0,3", "1,2,1,2", "1,2,2,1", "1,2,3,0", "1,2,4,7", "1,2,5,6", "1,3,0,2", "1,3,1,3", "1,3,2,0", "1,3,3,1", "1,3,4,6", "1,3,5,7");

	
var Ba1c = document.f.a1.checked;
var Bac = new Array(1);
Bac[0] = Ba1c;

var Bb1c = document.f.b1.checked;
var Bb2c = document.f.b2.checked;
var Bb3c = document.f.b3.checked;
var Bbc = new Array(Bb1c, Bb2c, Bb3c);

var Bc1c = document.f.c1.checked;
var Bc2c = document.f.c2.checked;
var Bc3c = document.f.c3.checked;
var Bc4c = document.f.c4.checked;
var Bc5c = document.f.c5.checked;
var Bcc = new Array(Bc1c, Bc2c, Bc3c, Bc4c, Bc5c);

var Bd1c = document.f.d1.checked;
var Bd2c = document.f.d2.checked;
var Bd3c = document.f.d3.checked;
var Bd4c = document.f.d4.checked;
var Bd5c = document.f.d5.checked;
var Bd6c = document.f.d6.checked;
var Bd7c = document.f.d7.checked;
var Bdc = new Array(Bd1c, Bd2c, Bd3c, Bd4c, Bd5c, Bd6c, Bd7c);

function ligneA()//interne a analyse()
{
var Atext = "The computer checked the button in line A.\nIt\'s your turn.";
	if (isFr) {Atext = "L\'ordinateur a choisi le bouton dans la ligne A.\nA vous de jouer !"}
document.f.txt.value = Atext;
document.f.a1.checked=true;
document.f.txtA.value=0;
document.f.txtAa.value=0;
document.f.txtL2.value="";
}//fin ligneA()

function button_s(x)
{
var a = " button";
	if (x>1) {a += "s";}
return a;
}

function bouton_s(x)
{
var a = " bouton";
	if (x>1) {a += "s";}
return a;
}

function ligneB(Bacocher)//interne a analyse()
{
var Btext = "The computer checked " + Bacocher + button_s(Bacocher) + " in the line B.\nIt\'s your turn.";
	if (isFr) {Btext = "L\'ordinateur a choisi " + Bacocher + bouton_s(Bacocher) + " dans la ligne B.\nA vous de jouer !";}
document.f.txt.value = Btext;
var Bacocher2 = Bacocher;
	for (var lB=1; lB<=Bacocher; lB++)
	{
		for (var ii=0; ii<3; ii++)
		{
			if (Bbc[ii]==0 && Bacocher2>0)
			{
			Bacocher2 -= 1;
				if (ii==0) {document.f.b1.checked=true;
				Bb1c=true;}
				if (ii==1) {document.f.b2.checked=true;
				Bb2c=true;}
				if (ii==2) {document.f.b3.checked=true;
				Bb3c=true;}
			}
		}
	}
document.f.txtB.value=eval(b-Bacocher);
document.f.txtBa.value=eval(b-Bacocher);
document.f.txtL2.value ="";
}//fin ligneB(Bacocher)

function ligneC(Cacocher)//interne a analyse()
{
var Ctext = "The computer checked " + Cacocher + button_s(Cacocher) + " in the line C.\nIt\'s your turn.";
	if (isFr) {Ctext = "L\'ordinateur a choisi " + Cacocher + bouton_s(Cacocher) + " dans la ligne C.\nA vous de jouer !";}
document.f.txt.value = Ctext;
var Cacocher2 = Cacocher;
	for(var lC=1; lC<=Cacocher; lC++)
	{
		for (var jj=0; jj<5; jj++)
		{
			if (Bcc[jj]==0 && Cacocher2>0)
			{
			Cacocher2 -= 1;
				if (jj==0) {document.f.c1.checked=true;
				Bc1c=true;}
				if (jj==1) {document.f.c2.checked=true;
				Bc2c=true;}
				if (jj==2) {document.f.c3.checked=true;
				Bc3c=true;}
				if (jj==3) {document.f.c4.checked=true;
				Bc4c=true;}
				if (jj==4) {document.f.c5.checked=true;
				Bc5c=true;}
			}
		}
	}
document.f.txtL2.value ="";
document.f.txtC.value=eval(c-Cacocher);
document.f.txtCa.value=eval(c-Cacocher);
}//fin ligneC(Cacocher)

function ligneD(Dacocher)//interne a analyse()
{
var Dtext = "The computer checked " + Dacocher + button_s(Dacocher) + " in the line D.\nIt\'s your turn.";
	if (isFr) {Dtext = "L\'ordinateur a choisi " + Dacocher + bouton_s(Dacocher) + " dans la ligne D.\nA vous de jouer !";}
document.f.txt.value = Dtext;
var Dacocher2 = Dacocher;
	for(var lD=1; lD<=Dacocher; lD++)
	{
		for (var kk=0; kk<7; kk++)
		{
			if (Bdc[kk]==0 && Dacocher2>0)
			{
			Dacocher2 -= 1;
				if (kk==0) {document.f.d1.checked=true;
				Bd1c=true;}
				if (kk==1) {document.f.d2.checked=true;
				Bd2c=true;}
				if (kk==2) {document.f.d3.checked=true;
				Bd3c=true;}
				if (kk==3) {document.f.d4.checked=true;
				Bd4c=true;}
				if (kk==4) {document.f.d5.checked=true;
				Bd5c=true;}
				if (kk==5) {document.f.d6.checked=true;
				Bd6c=true;}
				if (kk==6) {document.f.d7.checked=true;
				Bd7c=true;}
			}
		}
	}
document.f.txtL2.value ="";
document.f.txtD.value=eval(d-Dacocher);
document.f.txtDa.value=eval(d-Dacocher);
}//fin ligneD(Dacocher)

//le joueur perd la partie
function joueurPerd()//interne a analyse()
{
	if (abcd=="0,0,0,0") 
	{
	var perdu = "You lost the game.";
		if (isFr) {perdu = "Vous avez perdu la partie.";}
	alert(perdu);
	uneAutre();
	return true;
	}
return false;
}//fin joueurPerd()

//le joueur gagne la partie	
function joueurGagne()//interne a analyse()
{
	if (abcd=="1,0,0,0")
	{
	ligneA();
	return true;
	}
	if (abcd=="0,1,0,0")
	{
	ligneB(1);
	return true;
	}
	if (abcd=="0,0,1,0")
	{
	ligneC(1);
	return true;
	}
	if (abcd=="0,0,0,1")
	{
	ligneD(1);
	return true;
	}
return false;
}//fin joueurGagne()
var gagnetxt = "Congratulations!\nThe computer lost the game.";
	if (isFr) {gagnetxt = "Bravo !\nL\'ordinateur a perdu la partie.";}
if (joueurGagne()) {document.f.txt.value = ""; alert(gagnetxt); uneAutre();}

function posigagne() //interne a analyse()
{
//le joueur est en position gagnante
	if (!joueurPerd() && !joueurGagne())
	{
		for(var i=0; i<oui.length; i++)
		{
			if(abcd == oui[i])
			{
			//cocher un bouton dans une ligne au hasard
			var yadlaplace = "";
				if (Bac != "true")
				{yadlaplace += "A";}
				if (Bbc != "true,true,true")
				{yadlaplace += "B";}
				if (Bcc != "true,true,true,true,true")
				{yadlaplace += "C";}
				if (Bdc!= "true,true,true,true,true,true,true")
				{yadlaplace += "D";}
				var yena = yadlaplace;
				var yenaX = yena.length;
				var azar = Math.round((yenaX-1) *Math.random());
				if (yena.charAt(azar)=="A") {ligneA();}
				if (yena.charAt(azar)=="B") {ligneB(1);}
				if (yena.charAt(azar)=="C") {ligneC(1);}
				if (yena.charAt(azar)=="D") {ligneD(1);}
			// fin du hasard
			return true; 
			}
		}
	}
return false;
} // fin posigagne()

//l'ordinateur a un coup gagnant
function possible()//interne a analyse()
{
	if (!joueurPerd() && !joueurGagne() && !posigagne())
	{
	var aOK = "";
	var bOK = "";
	var cOK = "";
	var dOK = "";
		for(var i=0; i<oui.length; i++)
		{
		var ouiZ = oui[i];
		var ouiZ1 = ouiZ.charAt(0);
		var ouiZ2 = ouiZ.charAt(2);
		var ouiZ3 = ouiZ.charAt(4);
		var ouiZ4 = ouiZ.charAt(6);
			if (a==ouiZ1) {aOK += "y";}
			else { (a-ouiZ1>0) ? aOK+=(a-ouiZ1) : aOK += "n";}
			if (b==ouiZ2) {bOK += "y";}
			else { (b-ouiZ2>0) ? bOK+=(b-ouiZ2) : bOK += "n";}
			if (c==ouiZ3) {cOK += "y";}
			else { (c-ouiZ3>0) ? cOK+=(c-ouiZ3) : cOK += "n";}
			if (d==ouiZ4) {dOK += "y";}
			else { (d-ouiZ4>0) ? dOK+=(d-ouiZ4) : dOK += "n";}
		}

		for(var j=0; j<oui.length; j++)
		{
		var aOKj = aOK.charAt(j);
		var bOKj = bOK.charAt(j);
		var cOKj = cOK.charAt(j);
		var dOKj = dOK.charAt(j);

		var ligneacocher = "";
		// les boutons sont dans la ligne  A
		if(aOKj!="n"&&bOKj=="y"&&cOKj=="y"&&dOKj=="y")
		{ligneacocher += "A";}
		// les boutons sont dans la ligne  B
		if(aOKj=="y"&&bOKj!="n"&&cOKj=="y"&&dOKj=="y")
		{ligneacocher += "B"; var Bacocher = bOKj;}
		// les boutons sont dans la ligne  C
		if(aOKj=="y"&&bOKj=="y"&&cOKj!="n"&&dOKj=="y")
		{ligneacocher += "C"; var Cacocher = cOKj;}
		// les boutons sont dans la ligne  D
		if(aOKj=="y"&&bOKj=="y"&&cOKj=="y"&&dOKj!="n")
		{ligneacocher += "D"; var Dacocher = dOKj;}
		document.f.txtL2.value+= ligneacocher;
		}
		
		//il n'y a une ou plusieurs lignes
		var lesLignes = document.f.txtL2.value;
		if (lesLignes=="A") {ligneA();}
		if (lesLignes=="B" || (lesLignes.indexOf('D')==-1 && lesLignes.indexOf('C')==-1 && lesLignes.indexOf('B')!=-1 && lesLignes.length>1)) {ligneB(Bacocher);}
		if (lesLignes=="C" || (lesLignes.indexOf('D')==-1 && lesLignes.indexOf('C')!=-1 && lesLignes.length>1)) {ligneC(Cacocher);}
		if (lesLignes=="D" || (lesLignes.indexOf('D')!=-1 && lesLignes.length>1)) {ligneD(Dacocher);}
		
	}
}//fin possible
possible()
} // fin analyse()
analyse()
return false;
} // fin valide()

