function hangman() {

// This is the main game function 
// This function is attached to the guess button. 
// IT takes a letter input from a text area (making sure that the user has entered nothing else but a letter from the Alfa-bet (abcd ..)
// It is checking the letter against the current word that is set by the "restart" function below.
// The function also is counting the number of mistakes the player has made 
// and is drawing the corresponding hand Using the "drawHang" function
// The function is also keeping record of the so far used letters and is showing these in the "usedletters" text box
// and is making sure not to allow the player to use a particular letter more than once.
// The function is also keeping record of the so far guessed letters and is showing this to the player in the "word" text box.
// The function is also used to keep the score and show the score in the score div tag. 
// The function is also informing the user when the game is over 
// And is also making sure to stop after the game is over or has not begin yet and inform the user about that.


if (currentWord=="") {
	alert("Please click *Start* First");
	return false;
}

var abc="abcdefghijklmnopqrstuvwxyz";
var currentDash="";
var tempDash="";

currentDash=document.Game.word.value;

if (currentDash==currentWordtmp_) {
	alert("You saw the word already\nPress *Start* to play with different word")
	return false;
}


if (wrongchars==7) {
alert("You Lost already :)))\nClick *Show Word* to see the word\nClick *Start* to play again");
return false;
}


var CharEntered=document.Game.letter.value;
document.Game.letter.value="";
CharEntered=CharEntered.toLowerCase();

if (CharEntered=="") {
	alert("Please enter a single letter");
	document.Game.letter.focus();
	return false; 
}

if (abc.indexOf(CharEntered)==-1) {
	alert("Please letters from the Alfa-Bet Only");
	document.Game.letter.focus();
	return false;	
}

if (UsedChars.indexOf(CharEntered)!=-1) {
	alert("You have already used the letter: \n>>"+CharEntered+"<<");
	document.Game.letter.focus();
	return false;	
}

UsedChars+=CharEntered;
document.Game.usedletters.value=UsedChars;

if (currentWord.indexOf(CharEntered)==-1) {
	wrongchars+=1;
	alert("WRONG!!!\n>>"+CharEntered+"<<\n is not in our word\nYou have: "+(7-wrongchars)+" more Tries!");
}


if (currentWord.indexOf(CharEntered)!=-1) {
	alert("GOOD!!!\n>>"+CharEntered+"<<\n is in our word\nYou have: "+(7-wrongchars)+" more Tries!");
	for (count=0; count<currentWordtmp_.length; count++) {
		
		if (CharEntered==currentWordtmp_.charAt(count)) {
			tempDash+=CharEntered;
			continue;
		}
		tempDash+=currentDash.charAt(count);
	}
currentDash="";	
currentDash=tempDash;
document.Game.word.value=currentDash;
}

var scoreDivstr="";
var scoreDiv = document.getElementById("score");


var hangDiv = document.getElementById("hang");
hangDiv.innerHTML="";


if (wrongchars==0) {hangDiv.innerHTML=drawHang(wrongchars);}
if (wrongchars==1) {hangDiv.innerHTML=drawHang(wrongchars);}
if (wrongchars==2) {hangDiv.innerHTML=drawHang(wrongchars);}
if (wrongchars==3) {hangDiv.innerHTML=drawHang(wrongchars);}
if (wrongchars==4) {hangDiv.innerHTML=drawHang(wrongchars);}
if (wrongchars==5) {hangDiv.innerHTML=drawHang(wrongchars);}
if (wrongchars==6) {hangDiv.innerHTML=drawHang(wrongchars);}

if (wrongchars==7) {
hangDiv.innerHTML=drawHang(wrongchars);
alert("You Lose :)))\nClick *Show Word* to see the word\nClick *Start* to play again");
NoLose+=1;
scoreDiv.innerHTML="";
scoreDivstr+="<b>Wins: ";
scoreDivstr+=NoWin;
scoreDivstr+="<br>Looses: "+NoLose;
scoreDivstr+="</b>";
scoreDiv.innerHTML+=scoreDivstr;
return false;

}


if (currentDash==currentWordtmp_) {

alert("Congratulations!!!\nYou win!!!\nThis is the Word:\n>>"+currentWord+"<<\nPress *Start* to play again");
NoWin+=1;
scoreDiv.innerHTML="";
scoreDivstr+="<b>Wins: ";
scoreDivstr+=NoWin;
scoreDivstr+="<br>Looses: "+NoLose;
scoreDivstr+="</b>";
scoreDiv.innerHTML+=scoreDivstr;
return false;

}

return true;

}




function restart(spPart) {

// This function is used to set a new word the will be played with
// also draw the initial hang
// set the wrong letters number to 0
// writes the initial number of dashes in the word text box, so the user can see how many characters the word has
// This function is also setting a random word from the corresponding set (depending on the "spPart" value) in the "wordsStr" function
// It is also making sure that until all the words are used in the corresponding set. It will get a different random word.
// After all the words has been used from the corresponding set. The function is alerting the user and starts the process again.

var hangDivStr1="<pre>";
hangDivStr1+="_______||<br>";
hangDivStr1+="       ||<br>";
hangDivStr1+="       ||<br>";
hangDivStr1+="       ||<br>";
hangDivStr1+="       ||<br>";
hangDivStr1+="       ||<br>";
hangDivStr1+="       <a href='javascript:hint();'>||</a>";
hangDivStr1+="</pre>";

var hangDiv1 = document.getElementById("hang");
hangDiv1.innerHTML="";
hangDiv1.innerHTML=hangDivStr1;

var count=0;
var Initialdashes="";
currentWordtmp_="";

var words = new Array();
words=wordsStr(spPart);
UsedChars="";
currentDash="";
document.Game.usedletters.value="";
document.Game.letter.value="";

if ((wordsPlayedNou.length==words.length) && (spPart==1)) {
	alert("You have played with all the pre-inserted Nouns!\nNow, you will start to repeat them!");
	wordsPlayedNou=new Array();
}


if ((wordsPlayedVer.length==words.length) && (spPart==2)) {
	alert("You have played with all the pre-inserted Verbs!\nNow, you will start to repeat them!");
	wordsPlayedVer=new Array();
}



var wordNowNo = Math.floor(Math.random()*(words.length))				/* This is taking a random word from the array above */

if (spPart==1) {
	while (checkNum(wordNowNo, wordsPlayedNou)) {
		wordNowNo = Math.floor(Math.random()*(words.length))
	}
}

if (spPart==2) {
	while (checkNum(wordNowNo, wordsPlayedVer)) {
		wordNowNo = Math.floor(Math.random()*(words.length))
	}
}

currentWord = words[wordNowNo];

for (count=0; count<currentWord.length; count++) {Initialdashes+="- ";}

for (count=0; count<currentWord.length; count++) {currentWordtmp_+=currentWord.charAt(count)+" ";}

document.Game.word.value=Initialdashes;






if (spPart==1) {

wordsPlayedNou[wordsPlayedNou.length]=wordNowNo;
alert("Now you will play with - Nouns!\nYou have "+(words.length-wordsPlayedNou.length)+" unused left!\nThis one has total of:\n>>"+currentWord.length+"<< characters.\nHave Fun!!!");

}


if (spPart==2) {

wordsPlayedVer[wordsPlayedVer.length]=currentWord;
alert("Now you will play with - Verbs!\nYou have "+(words.length-wordsPlayedVer.length)+" unused left!\nThis one has total of:\n>>"+currentWord.length+"<< characters.\nHave Fun!!!");

}


wrongchars=0;

return true;
}





function drawHang(whatH) {

// This Function is used to draw the hangman and is used in the hangman function where 
// "whatH" gets the value of the number of wrong character so far (during the current word).   

var hangDivStr="";


if (whatH==0) {
	hangDivStr+="<pre>";
	hangDivStr+="_______||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       <a href='javascript:hint();'>||</a>";
	hangDivStr+="</pre>";
	return hangDivStr;
}

if (whatH==1) {
	hangDivStr+="<pre>";
	hangDivStr+="_______||<br>";
	hangDivStr+="  |    ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       <a href='javascript:hint();'>||</a>";
	hangDivStr+="</pre>";
	return hangDivStr;
}

if (whatH==2) {
	hangDivStr+="<pre>";
	hangDivStr+="_______||<br>";
	hangDivStr+="  |    ||<br>";
	hangDivStr+="  0    ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       <a href='javascript:hint();'>||</a>";
	hangDivStr+="</pre>";
	return hangDivStr;
}

if (whatH==3) {
	hangDivStr+="<pre>";
	hangDivStr+="_______||<br>";
	hangDivStr+="  |    ||<br>";
	hangDivStr+="  0    ||<br>";
	hangDivStr+="  |    ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       <a href='javascript:hint();'>||</a>";
	hangDivStr+="</pre>";
	return hangDivStr;
}

if (whatH==4) {
	hangDivStr+="<pre>";
	hangDivStr+="_______||<br>";
	hangDivStr+="  |    ||<br>";
	hangDivStr+="  0    ||<br>";
	hangDivStr+=" {|    ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       <a href='javascript:hint();'>||</a>";
	hangDivStr+="</pre>"
	return hangDivStr;
}

if (whatH==5) {
	hangDivStr+="<pre>";
	hangDivStr+="_______||<br>";
	hangDivStr+="  |    ||<br>";
	hangDivStr+="  0    ||<br>";
	hangDivStr+=" {|}   ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       ||<br>";
	hangDivStr+="       <a href='javascript:hint();'>||</a>";
	hangDivStr+="</pre>";     
	return hangDivStr;
}

if (whatH==6) {
	hangDivStr+="<pre>";
	hangDivStr+="_______||<br>";
	hangDivStr+="  |    ||<br>";
	hangDivStr+="  0    ||<br>";
	hangDivStr+=" {|}   ||<br>";
	hangDivStr+="  ^    ||<br>";
	hangDivStr+=" ^     ||<br>";
	hangDivStr+="       <a href='javascript:hint();'>||</a>";
	hangDivStr+="</pre>";
	return hangDivStr;
}

if (whatH==7) {
//	hangDivStr+="<font color='#FF0000'>";
	hangDivStr+="<pre>";
	hangDivStr+="_______||<br>";
	hangDivStr+="<font color='#FF0000'>  |    </font>||<br>";
	hangDivStr+="<font color='#FF0000'>  0    </font>||<br>";
	hangDivStr+="<font color='#FF0000'> {|}   </font>||<br>";
	hangDivStr+="<font color='#FF0000'>  ^    </font>||<br>";
	hangDivStr+="<font color='#FF0000'> ^ ^   </font>||<br>";
	hangDivStr+="<font color='#FF0000'> RIP   </font><a href='javascript:hint();'>||</a>";
	hangDivStr+="</pre>";
//	hangDivStr+="</font>";
	return hangDivStr;
}



}


function ShowWord1() {

// This function is used to show the word anytime during the game if the player presses the corresponding button.

if (currentWord=="") {
	alert("Please click *Start* First");
	return false;
}


document.Game.word.value="";
document.Game.word.value=currentWordtmp_;
alert("This is the word:\n>>"+currentWord+"<<\nClick *Start* to play with different word");
return true;

}




function ResScore() {

// This function is used to reset the score and assign the 0 : 0 score to the corresponding div tag 

var scoreDivstr="";
var scoreDiv = document.getElementById("score");
scoreDiv.innerHTML="";

NoLose=0;
NoWin=0;

scoreDivstr+="<b>Wins: ";
scoreDivstr+=NoWin;
scoreDivstr+="<br>Looses: "+NoLose;
scoreDivstr+="</b>";
scoreDiv.innerHTML+=scoreDivstr;

alert("The score is set to 0 : 0 !\nHave Fun!!!");
return true;

}





function changeFocus() {

// This function is used to move the focus out from the "word" and the "used letters" text boxes. 
// This is done by attaching this function to the "onFocus" attribute of the text boxes.

alert("Do not tuch Here!!!\n\nClick *Start* or enter a letter and click *Guess*");
document.Game.letter.focus();

}




function hint() {
if (currentWord!="") {
	alert("Cooooool!!!\nYou found the hidden HINT\nThis is the word:\n\n>>"+currentWord+"<<");
}
document.Game.letter.focus();
}



function checkNum(Num1, Array1) {

// This function is getting a number and an array and is cheking if the numebr is equal to any of the elements in the array
// If the above condition is true the function returns true. If not the function returns False
// It is used in the "restart" function to make sure that each time we get a random differnt word.

var count=0;

for (count=0; count<Array1.length; count++) {
	if (Array1[count]==Num1) {
		return true;
	}
}

return false;

}
	