//<![CDATA[
<!--


var nomsurfeur=prompt("Entrez vos nom et prénom, SVP","???   ???");
if ((nomsurfeur=="")||(nomsurfeur=="???   ???")) {nomsurfeur='Surfeur Anonyme'}
document.write("<center><h2>Bienvenue "+nomsurfeur+"</h2></center>");

// EclipseCrossword and this script block (C) Copyright 2000-2006 Green Eclipse except where otherwise noted.
// Do not remove this copyright notice.  You can, however, change the rest of the page.
// www.eclipsecrossword.com

var BadChars = "`~!@^*()_={[}]\|:;\"',<.>/?";

var CrosswordWidth, CrosswordHeight;
var TableAcrossWord, TableDownWord;
var Words, Word, Clue, AnswerHash, WordX, WordY, LastHorizontalWord;
var OnlyCheckOnce;

var CurrentWord, PrevWordHorizontal, x, y, i, j;
var CrosswordFinished, Initialized;

// Check the user's browser and then initialize the puzzle.
if (document.getElementById("waitmessage") != null)
{
	document.getElementById("waitmessage").innerHTML = "Merci de patienter...";
	
	// Current game variables
	CurrentWord = -1;
	PrevWordHorizontal = false;
	
// The following values are generated by EclipseCrossword.  This section is not copyrighted by Green Eclipse.
	CrosswordWidth = 23;
	CrosswordHeight = 25;
	Words = 30;
	WordLength = new Array(8, 8, 7, 6, 4, 4, 3, 5, 6, 7, 11, 9, 8, 8, 7, 5, 10, 10, 6, 7, 4, 9, 6, 5, 7, 10, 5, 8, 6, 10);
	Word = new Array();
	Clue = new Array("poète et probablement évêque d’Auch au Vème siècle.", 
"Bateaux à fond plat de certains envahisseurs.", 
"Adjectif qui désigne la loi franque.", 
"Charles, de son prénom, arrêta les Arabes à Poitiers en 732.", 
"Certains peuples ont fui devant ces terribles barbares venus de l’Est.", 
"Paysan qui n’est pas libre.", 
"L’€uro de l’époque.", 
"Ils étaient les « préfets » de Charlemagne et inspectaient son royaume pour lui.", 
"Son empire fut partagé en 843 par le traité de … ?", 
"Nom du prophète musulman.", 
"Il fut couronné empereur en 800.", 
"Autre nom que l’on donne aux Arabes qui envahirent le sud de la France au VIIIème siècle (714).", 
"Femme d’un roi qui se fit baptiser par amour pour elle.", 
"nom que l’on donne aux différentes populations qui envahirent la Gaule.", 
"Nom du bâtiment dans lequel les musulmans prient.", 
"Ville du baptême de Clovis.", 
"C’est une peinture ou un dessin exécuté à la main, qui décore ou illustre un texte la plupart du temps manuscrit. (Voir p.76 hist.  HATIER) Les techniques de l'imprimerie et de la gravure feront presque disparaître l'enluminure. Toutefois, il existe quelques livres imprimés qui en sont ornés.", 
"Témoignage du passé médiéval et conservée à Bayeux.", 
"Terre appartenant au seigneur, mais cultivée par un paysan.", 
"Travaux dûs aux seigneurs par les paysans.", 
"Nom de l’impôt versé à l’église.", 
"Adjectif qualifiant certains rois qui laissèrent les maires du palais gouverner à leur place.", 
"Lieu où vivent les abbés.", 
"Religion des musulmans.", 
"Venus du nord, ils ravagèrent la France  au  IXème siècle.", 
"Lieux où vivent les moines.", 
"Nombre de catégories sociales à l’époque du Moyen-âge.", 
"Autre nom du noble.", 
"Premier roi chrétien de France.", 
"Dessin décorant les livres d’époque.");
	AnswerHash = new Array(5985, 17139, 41072, 12283, 14386, 22378, 95252, 40986, 95996, 27513, 6729, 77781, 63857, 64025, 76190, 46472, 75595, 15188, 67348, 57687, 85505, 79626, 62909, 51900, 91134, 91230, 22666, 90091, 66606, 71759);
	WordX = new Array(13, 6, 8, 2, 8, 4, 0, 10, 1, 16, 7, 10, 9, 4, 13, 16, 2, 11, 18, 0, 6, 22, 17, 14, 1, 20, 10, 12, 7, 16);
	WordY = new Array(1, 2, 4, 5, 6, 7, 8, 9, 10, 10, 12, 18, 23, 0, 0, 0, 1, 1, 1, 2, 2, 3, 7, 9, 10, 10, 11, 11, 12, 15);
	LastHorizontalWord = 12;
	OnlyCheckOnce = true;
// End of non-copyrighted section.  The rest of this script block is (C) Green Eclipse.

	// Create the cell-to-word arrays.
	TableAcrossWord = new Array(CrosswordWidth);
	for (var x = 0; x < CrosswordWidth; x++) TableAcrossWord[x] = new Array(CrosswordHeight);
	TableDownWord = new Array(CrosswordWidth);
	for (var x = 0; x < CrosswordWidth; x++) TableDownWord[x] = new Array(CrosswordHeight);
	for (var y = 0; y < CrosswordHeight; y++)
		for (var x = 0; x < CrosswordWidth; x++)
		{
			TableAcrossWord[x][y] = -1;
			TableDownWord[x][y] = -1;
		}
	
	// First, add the horizontal words to the puzzle.
	for (var i = 0; i <= LastHorizontalWord; i++)
	{
		x = WordX[i];
		y = WordY[i];
		for (var j = 0; j < WordLength[i]; j++)
		{
			TableAcrossWord[x + j][y] = i;
		}
	}
	
	// Second, add the vertical words to the puzzle.
	for (var i = LastHorizontalWord + 1; i < Words; i++)
	{
		x = WordX[i];
		y = WordY[i];
		for (var j = 0; j < WordLength[i]; j++)
		{
			TableDownWord[x][y + j] = i;
		}
	}
	
	// Now, insert the row HTML into the table.
	for (var y = 0; y < CrosswordHeight; y++)
	{
		document.writeln("<tr>");
		for (var x = 0; x < CrosswordWidth; x++)
		{
			if (TableAcrossWord[x][y] >= 0 || TableDownWord[x][y] >= 0)
				document.write("<td id=\"c" + PadNumber(x) + PadNumber(y) + "\" class=\"box boxnormal_unsel\" onclick=\"SelectThisWord(event);\">&nbsp;</td>");
			else
				document.write("<td></td>");
		}
		document.writeln("</tr>");
	}
	
	// Finally, show the crossword and hide the wait message.
	Initialized = true;
	document.getElementById("waitmessage").style.display = "none";
	document.getElementById("crossword").style.display = "block";
}

// ----------
// Event handlers

// Raised when a key is pressed in the word entry box.
function WordEntryKeyPress(event)
{
	if (CrosswordFinished) return;
	// Treat an Enter keypress as an OK click.
	if (CurrentWord >= 0 && event.keyCode == 13) OKClick();
}

// ----------
// Helper functions

// Returns true if the string passed in contains any characters prone to evil.
function ContainsBadChars(theirWord)
{
	for (var i = 0; i < theirWord.length; i++)
		if (BadChars.indexOf(theirWord.charAt(i)) >= 0) return true;
	return false;
}

// Pads a number out to three characters.
function PadNumber(number)
{
	if (number < 10)
		return "00" + number;
	else if (number < 100)
		return "0" + number;
	else
		return "" +  number;
}

// Returns the table cell at a particular pair of coordinates.
function CellAt(x, y)
{
	return document.getElementById("c" + PadNumber(x) + PadNumber(y));
}

// Deselects the current word, if there's a word selected.  DOES not change the value of CurrentWord.
function DeselectCurrentWord()
{
	if (CurrentWord < 0) return;
	var x, y, i;
	
	document.getElementById("answerbox").style.display = "none";
	ChangeCurrentWordSelectedStyle(false);
	CurrentWord = -1;
	
}

// Changes the style of the cells in the current word.
function ChangeWordStyle(WordNumber, NewStyle)
{
	if (WordNumber< 0) return;
	var x = WordX[WordNumber];
	var y = WordY[WordNumber];
	
	if (WordNumber<= LastHorizontalWord)
		for (i = 0; i < WordLength[WordNumber]; i++)
			CellAt(x + i, y).className = NewStyle;
	else
		for (i = 0; i < WordLength[WordNumber]; i++)
			CellAt(x, y + i).className = NewStyle;
}

// Changes the style of the cells in the current word between the selected/unselected form.
function ChangeCurrentWordSelectedStyle(IsSelected)
{
	if (CurrentWord < 0) return;
	var x = WordX[CurrentWord];
	var y = WordY[CurrentWord];
	
	if (CurrentWord <= LastHorizontalWord)
		for (i = 0; i < WordLength[CurrentWord]; i++)
			CellAt(x + i, y).className = CellAt(x + i, y).className.replace(IsSelected ? "_unsel" : "_sel", IsSelected ? "_sel" : "_unsel");
	else
		for (i = 0; i < WordLength[CurrentWord]; i++)
			CellAt(x, y + i).className = CellAt(x, y + i).className.replace(IsSelected ? "_unsel" : "_sel", IsSelected ? "_sel" : "_unsel");
}

// Selects the new word by parsing the name of the TD element referenced by the 
// event object, and then applying styles as necessary.
function SelectThisWord(event)
{
	if (CrosswordFinished) return;
	var x, y, i, TheirWord, TableCell;
	
	// Deselect the previous word if one was selected.
	document.getElementById("welcomemessage").style.display = "none";
	if (CurrentWord >= 0) OKClick();
	DeselectCurrentWord();
	
	// Determine the coordinates of the cell they clicked, and then the word that
	// they clicked.
	var target = (event.srcElement ? event.srcElement: event.target);
	x = parseInt(target.id.substring(1, 4), 10);
	y = parseInt(target.id.substring(4, 7), 10);
	
	// If they clicked an intersection, choose the type of word that was NOT selected last time.
	if (TableAcrossWord[x][y] >= 0 && TableDownWord[x][y] >= 0)
		CurrentWord = PrevWordHorizontal ? TableDownWord[x][y] : TableAcrossWord[x][y];
	else if (TableAcrossWord[x][y] >= 0)
		CurrentWord = TableAcrossWord[x][y];
	else if (TableDownWord[x][y] >= 0)
		CurrentWord = TableDownWord[x][y];

	PrevWordHorizontal = (CurrentWord <= LastHorizontalWord);
	
	// Now, change the style of the cells in this word.
	ChangeCurrentWordSelectedStyle(true);
	
	// Then, prepare the answer box.
	x = WordX[CurrentWord];
	y = WordY[CurrentWord];
	TheirWord = "";
	var TheirWordLength = 0;
	for (i = 0; i < WordLength[CurrentWord]; i++)
	{
		// Find the appropriate table cell.
		if (CurrentWord <= LastHorizontalWord)
			TableCell = CellAt(x + i, y);
		else
			TableCell = CellAt(x, y + i);
		// Add its contents to the word we're building.
		if (TableCell.innerHTML != null && TableCell.innerHTML.length > 0 && TableCell.innerHTML != " " && TableCell.innerHTML.toLowerCase() != "&nbsp;")
		{
			TheirWord += TableCell.innerHTML.toUpperCase();
			TheirWordLength++;
		}
		else
		{
			TheirWord += "&bull;";
		}
	}
	
	document.getElementById("wordlabel").innerHTML = TheirWord;
	document.getElementById("wordinfo").innerHTML = ((CurrentWord <= LastHorizontalWord) ? "Across, " : "Down, ") + WordLength[CurrentWord] + " letters.";
	document.getElementById("wordclue").innerHTML = Clue[CurrentWord];
	document.getElementById("worderror").style.display = "none";
	document.getElementById("cheatbutton").style.display = (Word.length == 0) ? "none" : "";
	if (TheirWordLength == WordLength[CurrentWord])
		document.getElementById("wordentry").value = TheirWord;
	else
		document.getElementById("wordentry").value = "";
	
	// Finally, show the answer box.
	document.getElementById("answerbox").style.display = "block";
	try
	{
		document.getElementById("wordentry").focus();
		document.getElementById("wordentry").select();
	}
	catch (e)
	{
	}
	
}

// Called when the user clicks the OK link.
function OKClick()
{
	var TheirWord, x, y, i, TableCell;
	if (CrosswordFinished) return;
	if (document.getElementById("okbutton").disabled) return;
	
	// First, validate the entry.
	TheirWord = document.getElementById("wordentry").value.toUpperCase();
	if (TheirWord.length == 0)
	{
		DeselectCurrentWord();
		return;
	}
	if (ContainsBadChars(TheirWord))
	{
		document.getElementById("worderror").innerHTML = "Le mot écrit contient des caractères invalides.  SVP, n'écrire que des caractères autorisés comme ci-dessus.";
		document.getElementById("worderror").style.display = "block";
		return;
	}
	if (TheirWord.length < WordLength[CurrentWord])
	{
		document.getElementById("worderror").innerHTML  = "Pas assez de lettres.  Ce mot a " + WordLength[CurrentWord] + " lettres.";
		document.getElementById("worderror").style.display = "block";
		return;
	}
	if (TheirWord.length > WordLength[CurrentWord])
	{
		document.getElementById("worderror").innerHTML = "Trop de lettres.  Ce mot a " + WordLength[CurrentWord] + " lettres.";
		document.getElementById("worderror").style.display = "block";
		return;
	}
	
	// If we made it this far, they typed an acceptable word, so add these letters to the puzzle and hide the entry box.
	x = WordX[CurrentWord];
	y = WordY[CurrentWord];
	for (i = 0; i < TheirWord.length; i++)
	{
		TableCell = CellAt(x + (CurrentWord <= LastHorizontalWord ? i : 0), y + (CurrentWord > LastHorizontalWord ? i : 0));
		TableCell.innerHTML = TheirWord.substring(i, i + 1);
	}
	DeselectCurrentWord();
}

// Called when the "check puzzle" link is clicked.
function CheckClick()
{
	var i, j, x, y, UserEntry, ErrorsFound = 0, EmptyFound = 0, TableCell;
	if (CrosswordFinished) return;
	DeselectCurrentWord();
	
	for (y = 0; y < CrosswordHeight; y++)
	for (x = 0; x < CrosswordWidth; x++)
		if (TableAcrossWord[x][y] >= 0 || TableDownWord[x][y] >= 0)
		{
			TableCell = CellAt(x, y);
			if (TableCell.className == "box boxerror_unsel") TableCell.className = "box boxnormal_unsel";
		}
		
	for (i = 0; i < Words; i++)
	{
		// Get the user's entry for this word.
		UserEntry = "";
		for (j = 0; j < WordLength[i]; j++)
		{
			if (i <= LastHorizontalWord)
				TableCell = CellAt(WordX[i] + j, WordY[i]);
			else
				TableCell = CellAt(WordX[i], WordY[i] + j);
			if (TableCell.innerHTML.length > 0 && TableCell.innerHTML.toLowerCase() != "&nbsp;")
			{
				UserEntry += TableCell.innerHTML.toUpperCase();
			}
			else
			{
				UserEntry = "";
				EmptyFound++;
				break;
			}
		}
		// If this word doesn't match, it's an error.
		if (HashWord(UserEntry) != AnswerHash[i] && UserEntry.length > 0)
		{
			ErrorsFound++;
			ChangeWordStyle(i, "box boxerror_unsel");
		}
	}
	
		
	// If they can only check once, disable things prematurely. pascal
	if ( OnlyCheckOnce )
	{
		CrosswordFinished = true;
		document.getElementById("checkbutton").style.display = "none";
	}
	
	Note = (((parseInt(Words)-(parseInt(EmptyFound) + parseInt(ErrorsFound)))/parseInt(Words))* 20);
	Note = Note.toFixed(2);
	if (ErrorsFound > 0 && EmptyFound > 0)
		document.getElementById("welcomemessage").innerHTML = ErrorsFound + (ErrorsFound > 1 ?  " erreurs" :  " erreur") + " et " + EmptyFound + (EmptyFound > 1?  " mots inachevés" :  " mot inachevé") + (EmptyFound > 1?  " ont été trouvés." :  "a été trouvé.") + "  Notes: " + Note + " sur 20";
	else if (ErrorsFound > 0)
		document.getElementById("welcomemessage").innerHTML = ErrorsFound + (ErrorsFound > 1 ? " erreurs ont été" : " erreur a été") + (ErrorsFound > 1?  " trouvées" :  " trouvée") + "  Notes: " + Note + " sur 20";
	else if (EmptyFound > 0)
		document.getElementById("welcomemessage").innerHTML = "Aucune erreur trouvée, mais " + EmptyFound + (EmptyFound > 1?  " mots inachevés" :  " mot inachevé") + (EmptyFound > 1?  " ont été trouvés." :  "a été trouvé.") + "  Notes: " + Note + " sur 20";

	if (ErrorsFound + EmptyFound > 0)
	{
		document.getElementById("welcomemessage").style.display = "";
		return;
	}
			
	// They finished the puzzle!
	CrosswordFinished = true;
	document.getElementById("checkbutton").style.display = "none";
	document.getElementById("congratulations").style.display = "block";
	document.getElementById("welcomemessage").style.display = "none";
}

// Called when the "cheat" link is clicked.
function CheatClick()
{
	if (CrosswordFinished) return;
	var OldWord = CurrentWord;
	document.getElementById("wordentry").value = Word[CurrentWord];
	OKClick();
	ChangeWordStyle(OldWord, "box boxcheated_unsel");
}

// Returns a one-way hash for a word.
function HashWord(Word)
{
	var x = (Word.charCodeAt(0) * 719) % 1138;
	var Hash = 837;
	var i;
	for (i = 1; i <= Word.length; i++)
		Hash = (Hash * i + 5 + (Word.charCodeAt(i - 1) - 64) * x) % 98503;
	return Hash;
}

//-->
//]]>
