/* AUTEUR: Philippe Cuvinot */
/* DATE DE CREATION: 18/01/01 */


function getCookie (name)
{
	var dcookie = document.cookie;
	var cname = name + "=";
	var clen = dcookie.length;
	var cbegin = 0;
	while (cbegin < clen)
	{
		var vbegin = cbegin + cname.length;
		if (dcookie.substring(cbegin, vbegin) == cname)
		{
			var vend = dcookie.indexOf (";", vbegin);
			if (vend == -1) vend = clen;
				return unescape(dcookie.substring(vbegin, vend));
		}
		cbegin = dcookie.indexOf(" ", cbegin) + 1;
		if (cbegin == 0) break;
	}
	return null;
}

function setCookie (name, value, expires)
{
	if (!expires) expires = new Date();
	document.cookie = name + "=" + escape (value) + "; expires=" + expires.toGMTString() +  "; path=/";
}

function delCookie (name)
{
	var expireNow = new Date();
	document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}

//==============================================================================

//  Global Variables

var Alphabet = new initAlphaArray()
var NumOfWords = 42;
var SaveData = "";
var ImageNum = "";
var LettersSelected = "";
var RandomWord = "";
var DisplayWord = "";
var position = 0;
var word = new WordList();
var expdate = new Date();
var RandomNumber = (expdate.getSeconds())%NumOfWords;



function initAlphaArray()
{
	this.length = 26
	this[0] = "A"
	this[1] = "B"
	this[2] = "C"
	this[3] = "D"
	this[4] = "E"
	this[5] = "F"
	this[6] = "G"
	this[7] = "H"
	this[8] = "I"
	this[9] = "J"
	this[10] = "K"
	this[11] = "L"
	this[12] = "M"
	this[13] = "N"
	this[14] = "O"
	this[15] = "P"
	this[16] = "Q"
	this[17] = "R"
	this[18] = "S"
	this[19] = "T"
	this[20] = "U"
	this[21] = "V"
	this[22] = "W"
	this[23] = "X"
	this[24] = "Y"
	this[25] = "Z"
}



function WordList() 
{
	this.length = NumOfWords;    // allocate array space
	this[0] = "MACHU PICCHU";
	this[1] = "ISABEL ALLENDE";
	this[2] = "FRANCISCO PIZARRO";
	this[3] = "HERNAN CORTES";
	this[4] = "CHRISTOPHE COLOMB";
	this[5] = "MARIO VARGAS LLOSA";
	this[6] = "GABRIEL GARCIA MARQUEZ";
	this[7] = "VICENTE PINZON";
	this[8] = "AMERIGO VESPUCCI";
	this[9] = "SIMON BOLIVAR";
	this[10] = "FRANCISCO MIRANDA";
	this[11] = "ERNESTO CHE GUEVARA";
	this[12] = "CHICHEN ITZA";
	this[13] = "PALENQUE";
	this[14] = "TUPAC AMARU";
	this[15] = "ALEJO CARPENTIER";
	this[16] = "JORGE LUIS BORGES";
	this[17] = "SALVADOR ALLENDE";
	this[18] = "BUENOS AIRES";
	this[19] = "SANTO DOMINGO";
	this[20] = "OCTAVIO PAZ";
	this[21] = "JOSE SAN MARTIN";
	this[22] = "LUIS SEPULVEDA";
	this[23] = "MANCO CAPAC";
	this[24] = "HUAYNA CAPAC";
	this[25] = "HERNANDO DE SOTO";
	this[26] = "DIEGO DE ALMAGRO";
	this[27] = "CHICHICASTENANGO";
	this[28] = "PABLO NERUDA";
	this[29] = "BERNARDO DE SAHAGUN";
	this[30] = "DIEGO DE LANDA";
	this[31] = "TERRE DE FEU";
	this[32] = "EMILIANO ZAPATA";
	this[33] = "PANCHO VILLA";
	this[34] = "MIGUEL ANGEL ASTURIAS";
	this[35] = "JOSE MARIA ARGUEDAS";
	this[36] = "TEOTIHUACAN";
	this[37] = "SAN AGUSTIN";
	this[38] = "FIDEL CASTRO";
	this[39] = "CHAPULTEPEC";
	this[40] = "CHICHIMEQUE";
	this[41] = "TOTONAQUE";
}

function availableLetters(i)
{
	if (LettersSelected.charAt(i)==Alphabet[i])
		document.write('<TD ALIGN=CENTER VALIGN=CENTER WIDTH=20 HEIGHT=12>' +
          	'<B><A HREF="javascript:LoadNextPage('+i+',\''+Alphabet[i]+
			'\')">'+Alphabet[i]+'</A></B></TD>');
  	else
		document.write('<TD ALIGN=CENTER VALIGN=CENTER WIDTH=20 HEIGHT=12> </TD>');
}

function LoadNextPage(selected,letter)
{
	if (RandomWord.indexOf(letter) == -1)
	{
		if (ImageNum == "A") ImageNum = "B";
		else if (ImageNum =="B") ImageNum = "C";
		else if (ImageNum =="C") ImageNum = "D";
		else if (ImageNum =="D") ImageNum = "E";
		else if (ImageNum =="E") ImageNum = "F";
		else if (ImageNum =="F") ImageNum = "G";
		else if (ImageNum =="G") ImageNum = "H";
		else if (ImageNum =="H") ImageNum = "I";
		else if (ImageNum =="I") ImageNum = "J";
		else if (ImageNum =="J") ImageNum = "K";
		else ImageNum = "A";
	}

	var j=0;
	var HoldLettersSelected = LettersSelected;
	LettersSelected = "";
	if (selected == 0)
	{
		for (j=1; j<=25; j++)
		{
	     	LettersSelected += HoldLettersSelected.charAt(j);
		}
		LettersSelected = "^" + LettersSelected;
	}
	else if (selected == 25)
	{
		for (j=0; j<=24; j++)
		{
			LettersSelected += HoldLettersSelected.charAt(j);
		}
		LettersSelected += "^";
	}
	else
	{
		for (j=0; j<selected; j++)
		{
			LettersSelected += HoldLettersSelected.charAt(j);
		}
		LettersSelected += "^";
		for (j=selected+1; j<=25; j++)
		{
			LettersSelected += HoldLettersSelected.charAt(j);
		}
	}
	SaveData = ImageNum + LettersSelected + RandomWord + "*";
	setCookie("_HangMan", SaveData, expdate);
	history.go(0);
}

// Main Code

// Set cookie to expire in 10 days
//
// 1000 milliseconds  (milliseconds per second)
// * 60 milliseconds  (seconds per minute)
// * 60 milliseconds  (minutes per hour)
// * 24 milliseconds  (hours per day)
// * 10 milliseconds  (days)
// -----------------
// = 10 days
//
expdate.setTime (expdate.getTime() + (1000*60*60*24*10));

// Cookie does not exist processing
if(getCookie("_HangMan") == null)
{
	ImageNum = "A";
	LettersSelected = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	RandomWord = word[RandomNumber];
	SaveData = ImageNum + LettersSelected + RandomWord + "*";
	setCookie("_HangMan", SaveData, expdate);
}

// Cookie exists processing
else
{
	SaveData = getCookie("_HangMan");
	ImageNum = SaveData.charAt(0);
	for (position=1; position<=26; position++)
	{
		LettersSelected += SaveData.charAt(position);
	}
	for (position=27; position<SaveData.indexOf("*"); position++)
	{
		RandomWord += SaveData.charAt(position);
	}
}
// Calculate how the word the user is guessing should be displayed
DisplayWord = "";
for (i=0; i<RandomWord.length; i++)
{
	if (RandomWord.charAt(i) == ' ')
	{
		DisplayWord += " ";
	}
	else
	{
		MatchFound = false;
		for (j=0; j<=25; j++)
		{
			if ((LettersSelected.charAt(j) == "^") && (RandomWord.charAt(i) == Alphabet[j]))
			{
				DisplayWord += RandomWord.charAt(i);
				MatchFound = true;
			}
		}
		if (!MatchFound) DisplayWord += "-";
	}
}

// Draw Screen

document.write('<TITLE>Sauvez le Roi</TITLE>');
document.write('<BODY BGCOLOR="#FFFFFF"');
document.write('<IMG SRC="http://www.americas-fr.com/icones/logo-am.gif" BORDER=0 WIDTH=100 HEIGHT=95 ALT="Amerique Latine" ALIGN=absmiddle> <FONT SIZE=5 COLOR="#0000FF">Sauvez le Roi de la pendaison <BR>en trouvant le bon mot</FONT><BR><BR>');


document.write('<IMG HEIGHT=200 WIDTH=200 ALIGN=RIGHT ALT="Pendu"'+
	'SRC="http://www.americas-fr.com/jeux/pendu2/hangman'+ImageNum+'.gif" >');
document.write('<CENTER>');

if (ImageNum == "K")
{
	document.write('<FONT COLOR=RED SIZE=4>Vous avez perdu !<BR><BR>Réponse :  "' + RandomWord + '"</FONT>');
}
else if (RandomWord == DisplayWord)
{
	document.write('<FONT COLOR=RED SIZE=8>Vous avez gagné !</FONT>');
}
else
{
	document.write('<FONT SIZE=4>Choisissez une de ces lettres :</FONT><BR><BR>');
	document.write('<TABLE>');
	document.write('<TR>');

	for (i=0; i<13; i++) availableLetters(i);
		document.write('</TR>');
		document.write('<TR>');
	for (i=13; i<26; i++) availableLetters(i);
		document.write('</TR>');
		document.write('</TABLE>');
}

document.write('<BR>');
document.write('<BR>');
document.write('<FONT SIZE=9><TT>');
document.write(DisplayWord);
document.write('</TT></FONT>');
document.write('<FORM>');
document.write('<INPUT TYPE="button" VALUE="Rejouer"'+
	'ONCLICK="delCookie(\'_HangMan\');history.go(0);">');
document.write('<INPUT TYPE="button" VALUE="Sortir"'+
	'ONCLICK="delCookie(\'_HangMan\');history.go(-1);">');
document.write('</FORM>');
document.write('</CENTER>');
document.write('</BODY>');
