function numberofnewwords(NumberOfNewWord) {												/* starting the function */
												
// This function is prompting for integer and ensuring that you entered integer!
// It could be changed by changing the 'usNumbers' string to ensure that you entered any string that contains leteres or numbers from the 'usNumbers' string!


var usNumbers="0123456789";															/* Defining the correct numbers */
NumberOfNewWord=prompt("Enter the number of words to be changed: ", "NUMBER");									/* initial prompt for number */
while (NumberOfNewWord=="")  {NumberOfNewWord=prompt("Do not enter Blank: ", "NUMBER");}							/* keep asking until you enter something */
var temp="";																	/* defining value to be checked later in the FOR loop */
var c=0;																	/* defining the counter and setting it to 0 */
	for (c=0; c<(NumberOfNewWord.length); c++) {												/* starting the counter to check if we have blanks or letters in the entered string */
 		temp = NumberOfNewWord.charAt(c);												/* temp receives value of the corresponding (c-th) character of the string we entered starting from c=0 (the first character) */  
			if (usNumbers.indexOf(temp) == "-1" || temp == "") {									/* checking if the current (temp) character of the string is the above defined 'usNumbers' string, which is the set of allowed values in this case the numbers from 0 to 9. Or checking if it is blank */
				alert("You have to enter a number Only! \n\nYou entered: "+NumberOfNewWord);					/* if the above condition is true we alert the user for the mistake and showing him what he entered */
 				c=-1;														/* setting the counter to -1 so it can continue the for loop form 0, otherwise it will go to t++ and will move to the next character which might not be wrong */
				NumberOfNewWord=prompt("Please try again! Enter the number of words to be changed: ", "NUMBER ONLY");		/* prompting for new entry after a mistake is found */
				while (NumberOfNewWord=="")  {NumberOfNewWord=prompt("Do not enter Blank: ", "NUMBER");}			/* again checking for blank entry */
		}																/* closing the IF condition */
	}																	/* closing the FOR loop */
alert("Thank you! "+NumberOfNewWord+" words will be changed");											/* alerting the user that he entered a correct number and showing the number */
NumberOfNewWord=parseInt(NumberOfNewWord);													/* using the JavaScript function parseInt to convert the entry from string to integer */
return NumberOfNewWord;																/* returning the final result Integer */
}																		/* closing the function */











function DifRandInt(NumRandInt, DifRandArray, rangeMax) {										/* starting the function */

// This function creates an Array "DifRandArray" containing total of "NumRandInt" different random integers in the range from 0 to "rangeMax"!
// It ensures that rangeMax > NumRandInt otherwise it will not work and will return blank array!

	if (rangeMax<NumRandInt) {													/* We do the first check IF the range is greater than the numbers that we want to create */
		alert("This is impossible operation.\n You want to create "+NumRandInt+" different numbers,\n in the range from 0 to "+rangeMax+".\n Use this function with numbers where \n rangeMax("+rangeMax+")>NumRandInt("+NumRandInt+")!!!");	/* We alert the user of the mistake */
		DifRandArray=[""];													/* After the mistake the Array gets blank */
		return DifRandArray;													/* After the mistake the function returns the blank array */
	}																/* closing the IF statement */
var counter=0;																/* Declaring the first counter */
var counter1=0;																/* Declaring the second counter */
	for (counter=0; counter<NumRandInt; counter++)  {										/* Starting the first FOR loop for assigning initial total "NumRandInt" number of random values to the "DifRandArray" array in the range from 0 to "rangeMax" */
		DifRandArray[counter]=Math.floor(Math.random()*(rangeMax));								/* Assigning each element of the "DifRandArray" array a random number in the range from 0 to "rangeMax" */
	}																/* closing the first FOR loop */
	for (counter=0; counter<DifRandArray.length; counter++) {									/* Starting the second FOR loop which will start from the first element of the "DifRandArray" array and checking it for equality against the next elements using a nested FOR loop */
		for (counter1=(counter+1); counter1<DifRandArray.length; counter1++) {							/* Starting the nested FOR loop used to check for equality the corresponding element of the "DifRandArray" array against the remaining elements */ 
			if (DifRandArray[counter]==DifRandArray[counter1]) {								/* IF statement Performing the check described on the previous line */
				DifRandArray[counter]=Math.floor(Math.random()*(rangeMax))						/* IF a mistake (the corresponding element is equal to any of the remaining elements) is found the corresponding element is assigned a new random value in the range from 0 to rangeMax */
				counter=-1;												/* since the described above for loop is checking the corresponding element against the remaining elements and do not check against the elements before, IF a mistake (equality) is found the control of the initial FOR loop is set to -1 so when it goeas back to the maintainer it starts from 0 and checking again. This is performed until all elements of the array are different */
			}														/* Closing the IF statement */	
		}															/* Closing the nested FOR loop */
	}																/* Closing the second main FOR loop */
return DifRandArray;															/* Returning the final "DifRandArray" containing "NumRandInt" random different Integers in the range from 0 to rangeMax */
}																	/* Closing the Function */





function SortIntArray(IntArray) {													/* Starting the function */

// This function is used to sort a array containging integers and return a different array with the same elements, but sorted in accending order.

var counter=0;																/* Declaring a counter variable */
var tempInt=0;																/* Declaring a temp integer used later */
	for (counter=0; counter<IntArray.length; counter++) {										/* Starting a FOR loop to go through the array */
		tempInt=IntArray[counter];												/* The tempInt variable gets the value of the current array element */
		if (IntArray[counter]>IntArray[(counter+1)]) {										/* IF statement that checkes if the current array element is greater than the next one */
			IntArray[counter]=IntArray[(counter+1)];									/* IF the above condition is true this means that here the order is not correct and the current element gets the value of the next one which, is smaller */
			IntArray[(counter+1)]=tempInt;											/* after this the next element of the array gets the vale of tempInt that is actually the value of the current array element. With this 2 statements above we are effectively changing the places of 2 elements of the array if the first one is greater than the other */
			counter=-1;													/* Here we set the counter to -1 so when it goes back to the meintenence of the for loop (counter++) it actually becomes 0 and the we start cheking the array from the begining so no element is grater than the following one */
		}															/* Here we close the IF statement */
	}																/* Here we close the for statement */ 
	return IntArray;														/* After the arrangement is finished the function returns an array with the same elements as the input one but in the accending order */
}																	/* Here the function ends */





function DiffOnly(OriginalArray, SecondArray, cases) {											/* Here we start the function */

// This Function is used to get 2 Arrays and case (cases) as input
// and return in case 1 The number of different elements in the first array 
// in case 2 to return the first array with only the different elements lower casead
// in case 3 to return the second array cutted (elements removed) where the first array was cutted.
// We need this function in the Mad Lib game and we assume that the first input array is the Actuall text and the second is the corresponding speech parts. 
// This is why we only check the first array for equal elements and cut the corrsponding speech parts of the second array
// NOTE !!!! 
// We need this entire operation in Mad Lib because this version of the game generates random numbers of words to be changed
// and with this function we ensure that for example if we have one name in the text the game will not ask the player 2 times to change the same name 
// this is also valid for every other word in the text. This version of the game effectively asks the user to change a particular word 
// and ensures to change this word every time it is repeted in the entire Text. 
// It is easy to change this function a liitle and do this only for names for example by looking at the seech parts array, 
// but i decided that it will be more fun if we change every word that is repeted. This is the twist of the Mad Lib by Tsvyatko Dorovski

var OriginalArrayLowCase = new Array();													/* Here we declare the first array that will get the same elements as the first input array but lower case */
var SecondArrayLowCase = new Array();													/* Here we declare the second array that will get the same elements as the second input array but lower case */
var count=0;																/* Here we declare the first counter that will be used */
var count1=0;																/* Here we declare the second counter that will be used */
	for (count=0; count<OriginalArray.length; count++) {										/* Here we start a FOR loop that is used to give the first array the elements of the first input array, but lower case. We need to transform all elements to low case so we can compare them afterwards */
		OriginalArrayLowCase[count]=OriginalArray[count].toLowerCase();								/* Here the current element of the first array gets the current element of the first input array lower case */
	}																/* Here we close the FOR loop */
	for (count=0; count<SecondArray.length; count++) {										/* Here we start a FOR loop same as the above, but with the second array, the opearation is repeted in a different loop since the 2 input arrays might have different lenghts */
		SecondArrayLowCase[count]=SecondArray[count].toLowerCase();								/* Here the current element of the second array gets the current element of the second input array lower case */
	}																/* Here we close the FOR loop */
	for (count=0; count<OriginalArrayLowCase.length; count++) {									/* Here we start the for loop that will check each element in the first lower cased array for equality with the other elements and will remove any elemenst that repeats and will also remove the coresponding element of the second lower case array */ 
		for (count1=count+1; count1<OriginalArrayLowCase.length; count1++) {							/* Here we start a nested for loop so we can stop on the current element with the above loop and continue the check the other elements of the array. This is why the second counter count1 gets the the value of the first counter +1 so we do not check the current element value against its own  */
			if (OriginalArrayLowCase[count]==OriginalArrayLowCase[count1]) {						/* Here we do the IF statement that checks for equality the current element of the array from the first FOR loop against the remaining elements */  
				OriginalArrayLowCase.splice(count1,1);									/* If an equality is found the coresponding element is removed */
				SecondArrayLowCase.splice(count1,1);									/* If an equality is found the coresponding element of the second array is removed */
				count1-=1;												/* If an equality is found the second counter is moved back with 1 since the array is cut with one element and now for example (let's say the 3th element is equal to the 4th in this case the 4th element is removed and the 5th element becomes 4th) this is why we have to move the counter back with one because when it goes to the meintence it will become in our example 4+1 = 5 and will to jump through the 5th that became 4th (will miss the new 4th element) */
			}														/* Here we close the if statement */
		}															/* Here we close the nested FOR loop */
	}																/* Here we close the first FOR loop */
	for (count=0; count<OriginalArrayLowCase.length; count++) {									/* This FOR loop is used to remove the "**nl**tsd" string that is used for new lines. We do not want the user to be asked to change new line */
		if (OriginalArrayLowCase[count]=="**nl**tsd") {										/* Here we check each element of the text for the string although it will be only one after the above statements */
			OriginalArrayLowCase.splice(count,1);										/* Here we remove the special string from the text array */
			SecondArrayLowCase.splice(count,1);										/* Here we remove the special string from the speech parts array We are sure that it is on the same place because of a LOOP in the main game function where we add the tring in the same plcae of speechparts array where it is in the text array*/
			count-=1;													/* Here move the counter one back because the array has one element less after the above cut*/
		}															/* Here we close the IF statement */
	}																/* Here we close the FOR loop */
if (cases==1)	{return OriginalArrayLowCase.length;}											/* Here we do the first IF statement if the "cases" is equal to 1 we return the number of the lenght of the new cutted array (the numebr of different elements of the first input array) */
if (cases==2)	{return OriginalArrayLowCase;}												/* Here if the "cases" is equal to 2 we return an array that has only the different values of the first input array and lower case */
if (cases==3)	{return SecondArrayLowCase;}												/* Here if the "cases" is equal to 3 we return the second array cutted on the same place as the first one */
}																	/* Here we close the function */





function change(TextArray1, TextArrayDiff1, TextArraySp) {			/* Here we start the function */ 

// This function is used to change the words from the question text boxes to the.
// OriginalText Low case array and retrun a string in the selected text area
// It takes as input 
// NOTE!!! we need to have a form in our web page with the name "QuestionForm" that contains text boxes where
// the player enters the differnt words that will be changed in the original text 

var count=0;							/* Here we declare the first counter */
var count1=0;							/* Here we declare the second counter */
var TextArray = new Array();					/* Here we */
var TextArrayDiff = new Array();
var tempString="";
var tempString1="";
var result="";
	
	
	for (count=0; count<document.QuestionForm.elements.length; count++) {

		if (document.QuestionForm.elements[count].value=="") {
			alert("Speach part "+count+" is blank.\nYou have to enter something to continue");
			document.QuestionForm.elements[count].focus();
			return result;
		}

		if (document.QuestionForm.elements[count].name=="adverb" && document.QuestionForm.elements[count].value.substring((document.QuestionForm.elements[count].value.length-2),(document.QuestionForm.elements[count].value.length))!="ly") {
			alert("Speech part "+count+" is: "+document.QuestionForm.elements[count].name+" and has to end with -ly\nYou entered: "+document.QuestionForm.elements[count].value+" which, ends with -"+document.QuestionForm.elements[count].value.substring((document.QuestionForm.elements[count].value.length-2),(document.QuestionForm.elements[count].value.length))+"\nPlease change this word and continue");
			document.QuestionForm.elements[count].focus();
			return result;
		}

		
	
	}	

		
	
	
	
	for (count=0; count<TextArray1.length; count++) {
		TextArray[count]=TextArray1[count];
	}

	for (count=0; count<TextArrayDiff1.length; count++) {
		TextArrayDiff[count]=TextArrayDiff1[count];
	}


	for (count=0; count<TextArrayDiff.length; count++) {
		for (count1=0; count1<TextArray.length; count1++) {
			if (TextArrayDiff[count]==TextArray[count1]) {
				TextArray[count1]=document.QuestionForm.elements[count].value;
			}
		}
	}

	tempString=TextArray[0];
	tempString1=tempString.charAt(0).toUpperCase();       
	tempString1+=tempString.substring(1, tempString.length);
	TextArray[0]=tempString1;

	
	for (count=0; count<TextArray.length; count++) {
		if (TextArray[count-1]=="." || TextArray[count-1]=="!" || TextArray[count-1]=="?" || TextArraySp[count]=="name" || TextArraySp[count-1]=="**nl**tsd") {
			tempString=TextArray[count];
			tempString1=tempString.charAt(0).toUpperCase();       
			tempString1+=tempString.substring(1, tempString.length);
			TextArray[count]=tempString1;
		}
	
	}


	for (count=0; count<TextArray.length; count++) {
		if (TextArray[count]== "**nl**tsd") {
			result+= "\n\n";
			continue;
		}
		result+= TextArray[count]+" ";
	}

return result;
}





function reset1() {
// This function is used to reset the values of the question text boxes as well as the text area to blank
// NOTE!!! we need to have 2 forms in our web page first one called "QuestionForm" and containing text boxes 
// and second one called "TextForm" and containg a text area called "output"

var count=0;				/* Here we declare the counter */
	for (count=0; count<document.QuestionForm.elements.length; count++) {		/* Here we start a FOR loop that will go through the elements of the "QuestionForm" form and will set them to "" - blank */ 
		document.QuestionForm.elements[count].value="";				/* Here the coresponding element is gets blank - "" */	
	}										/* Here we close the FOR loop */
document.TextForm.output.value="";							/* Here the value of the "output" text area of the "TextForm" also gets blank - "" */
return true;										/* Here we return someting so the function can be called from a form button */
}											/* Here we close the function */




function restore1() {
// This function restores the Original text and the speech parts to the text areas.


var text="";
var speach="";
var count=0;

for (count=0; count<OriginalText.length; count++) {
	if (OriginalText[count]=="**nl**tsd") {
		text+="\n\n";
		continue;
	}
text+=OriginalText[count]+" ";
}


for (count=0; count<SpeachParts.length; count++) {
	if (SpeachParts[count]=="**nl**tsd") {
		speach+="\n\n";
		continue;
	}
speach+=SpeachParts[count]+" ";
}

document.Initial.Original.value=text;

document.Initial.SpeachP.value=speach;

return true;

}





function checkText() {
var count1=0;
var count2=0;
var counter=0;
count1=OriginalText.length;
count2=SpeachParts.length;
	if (count1>count2) {
		for (counter=0; counter<count1; counter++) {
			alert("Word "+counter+" is: "+OriginalText[counter]+"\nSpeach Part "+counter+" is: "+SpeachParts[counter]);

		}
	}


	if (count1<count2) {
		for (counter=0; counter<count2; counter++) {
			alert("Word "+counter+" is: "+OriginalText[counter]+"\nSpeach Part "+counter+" is: "+SpeachParts[counter]);

		}
	}
	if (count1==count2) {
		alert("Your Text looks Fine or you haven't clicked *start* yet\n Clik *start* to continue");
	}
return true;
}



function removeNL(StringNL) {

// This function removes all consecutive new lines and leaves anly one 
// It than changes the Remaining new lines with the string  " **nl**tsd "  that is used later 
// to put them back in the output. It works for both in IE and Mozila
	
var temStr="";
var temStr1="";
var temStr2="";
var temStrMoz="";
var temStrIE="";

var count=0;
var ancii=0;
var ancii1=0;
var ancii2=0;
var ancii3=0;
temStr=StringNL;


	for (count=0; count<temStr.length; count++) {						/* This is for Mozila only where we have only 10 as new line in IE it will not do anything since we do not have there 2 ancii 10 one after the other */
		ancii  = temStr.charCodeAt(count);
		ancii1 = temStr.charCodeAt(count+1);
		ancii2 = temStr.charCodeAt(count+2);
		ancii3 = temStr.charCodeAt(count+3);
			
		if ( (ancii==10 && ancii1==10) ) {
			continue;
		}
			
	temStrMoz+=temStr.charAt(count);
			
		
	}


	
	for (count=0; count<temStrMoz.length; count++) {					/* This is for IE only where we have ancii 10 followed by ancii 13 for new line */
		ancii  = temStrMoz.charCodeAt(count);
		ancii1 = temStrMoz.charCodeAt(count+1);
		ancii2 = temStrMoz.charCodeAt(count+2);
		ancii3 = temStrMoz.charCodeAt(count+3);
		if ( (ancii==13 && ancii1==10 && ancii2==13 && ancii3==10) ) {
			count+=1;								/* this is to jump through the ancii 10 since in IE we have ancii 10 and ancii 13 for new line */
			continue;
		}
	temStrIE+=temStrMoz.charAt(count);
	}



	for (count=0; count<temStrIE.length; count++) {						/* This is for IE */
		ancii  = temStrIE.charCodeAt(count);
		ancii1 = temStrIE.charCodeAt(count+1);
		if ( (ancii==13 && ancii1==10) ) {
			temStr1+=" **nl**tsd ";
			count+=1;								/* This is to jump */
			continue;
		}
	temStr1+=temStrIE.charAt(count);
	}



	for (count=0; count<temStr1.length; count++) {						/* This is for Mozilla */
		ancii  = temStr1.charCodeAt(count);
		ancii1 = temStr1.charCodeAt(count+1);
		if ( (ancii==10) ) {
			temStr2+=" **nl**tsd ";
			continue;
		}
	temStr2+=temStr1.charAt(count);
	}



return temStr2;

}

















