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

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

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

function regle() {
var laregle = "cavalier/regle_eng.html";
	if (isFr) {laregle = "cavalier/regle.html";}
window.open(laregle,'regle','height=550, width=600, resizable, scrollbars');
}

var w = new Image();
w.src = "cavalier/w.gif";
var b = new Image();
b.src = "cavalier/b.gif";
var wn = new Image();
wn.src = "cavalier/wn.gif";
var bn = new Image();
bn.src = "cavalier/bn.gif";
var wp = new Image();
wp.src = "cavalier/wp.gif";
var bp = new Image();
bp.src = "cavalier/bp.gif";

var casesuivante = new Array ("21", "31", "41", "51", "61", "71", "81",
"82", "62", "32", "12",
"13", "23", "43", "53", "73", "83",
"84", "74", "64", "54", "44", "34", "24", "14",
"15", "35", "65", "85",
"86", "76", "56", "46", "26", "16",
"17", "27", "37", "47", "57", "67", "77", "87",
"88", "78", "68", "58", "48", "38", "28", "18");

var lettres = "abcdefgh";
var suivant = 0;
var coup = 0;

function validmove(x) {
var a = document.f.casearrivee.value * 1;
	if (x == a + 12
	|| x == a + 21
	|| x == a - 12
	|| x == a - 21
	|| x == a + 8
	|| x == a + 19
	|| x == a - 8
	|| x == a - 19) {return true;}
	else { return false;}
}

function quellecouleur(x) {
var couleur = "";
	((Math.floor(x/10)+x%10)%2 == 0) ? couleur = 'b' : couleur = 'w';
return couleur;
}

function temps()
{
var xSec = document.f.sec.value;
xSec++;
document.f.sec.value = xSec;
	if (xSec == 60)
	{
	document.f.mn.value++; document.f.sec.value = 0;
	}
	if (xSec > 60)
	{
	document.f.mn.value++; document.f.sec.value = (document.f.sec.value * 1) - 60;
	}
}

var z;
function chrono() {
stopit();
z = window.setInterval('temps()',1000);
}

function stopit() {
window.clearInterval(z);
}

function mymove(x) {
	if (!validmove(x)) {
		document.f.illegal.value++;
		document.f.sec.value = (document.f.sec.value * 1) + 10;
	}
	else {
	coup++;
	if (coup == 1) { chrono();}
	document.f.coup.value = coup;
	var imagedepart = "c" + document.f.casearrivee.value;
	var imagedepartsrc = quellecouleur(document.f.casearrivee.value) + ".src";
	var imagearrivee = "c" + x;
	var imagearriveesrc = quellecouleur(x) + "n.src";
	document[imagedepart].src = eval(imagedepartsrc);
	document[imagearrivee].src = eval(imagearriveesrc);
	document.f.casearrivee.value = x;
	//but
	var but = document.f.but.value;

//	for testing	if (but == 31 && x == 31) {

		if (but == 18 && x == 18) {
			stopit();
			var yourtime = document.f.mn.value + "'" + document.f.sec.value + "\"";
			var timeinsec = (document.f.mn.value * 60) + (document.f.sec.value * 1);
			document.cookie = "yourscore=" + escape ( timeinsec );
			var voila = "<br />You did it!<br /><button type=\"submit\">record your score<\/button>";
			if (isFr) {voila = "<br />Bravo !\nVous avez réussi&nbsp;!<br /><button type=\"submit\">enregistez votre score<\/button>";}
			document.getElementById("youdidit").innerHTML=voila;
			document.getElementById("youdidit").style.display="block";
		}
		else {
			if (x == but) {
				suivant++;
				but = casesuivante[suivant];
				document.f.but.value = but;
				document.f.butname.value = lettres.charAt(Math.floor(but/10)-1) + but%10;;
			}

		}
	}
}

var zz;

function solution() {
	zz = window.setInterval('playSolution()',500);
}

function endSolution() {
	window.clearInterval(zz);
}

var coupsSol = new Array(
	new Array(32,13,21),
	new Array(13,32,24,12,31),
	new Array(43,62,41),
	new Array(62,43,51),
	new Array(32,53,61),
	new Array(82,74,62,83,71),
	new Array(83,62,81),
	new Array(73,61,82),
	new Array(74,62),
	new Array(43,24,32),
	new Array(24,12),
	new Array(24,32,13),
	new Array(34,15,23),
	new Array(35,43),
	new Array(51,32,53),
	new Array(65,73),
	new Array(85,64,83),
	new Array(64,76,84),
	new Array(65,86,74),
	new Array(62,83,64),
	new Array(83,62,54),
	new Array(35,56,44),
	new Array(32,53,34),
	new Array(53,32,24),
	new Array(16,35,14),
	new Array(26,34,15),
	new Array(27,35),
	new Array(56,77,65),
	new Array(73,85),
	new Array(77,65,86),
	new Array(65,84,76),
	new Array(68,56),
	new Array(35,54,46),
	new Array(34,26),
	new Array(47,35,16),
	new Array(37,18,26,38,17),
	new Array(38,46,27),
	new Array(46,58,37),
	new Array(56,35,47),
	new Array(68,76,57),
	new Array(78,86,67),
	new Array(86,65,77),
	new Array(56,68,87),
	new Array(68,76,88),
	new Array(67,86,78),
	new Array(57,76,68),
	new Array(56,77,58),
	new Array(37,56,48),
	new Array(27,46,38),
	new Array(26,47,28),
	new Array(16,37)
	);

var numSerieSol = 0;
var numSol = 0;

function playSolution() {
	if (numSerieSol == coupsSol.length) {
		document['c37'].src = b.src;
		document['c18'].src = wn.src;
		endSolution();
		stopit();
		compteurSolution = 0;
		var finsol = "That's it";
			if (isFr) {finsol = "Super !";}
		document.f.coup.value = 151;
		alert(finsol);
		uneAutre();
	}
	else {
		if (numSol < coupsSol[numSerieSol].length) {
			mymove(coupsSol[numSerieSol][numSol]);
			numSol++;
		}
		else {
			numSerieSol++;		
			numSol = 0;
		}
	}
}
