//starting date:1st june 2018
var Aksharkhel = {};
var Utils = {};
Utils.Path = '';
Utils.mobileDeviceFlag = false;
var questionId = 0, startTime = 0, scoredMarks = 0, totalMarks = 10, resId = 0, level = 0;

$(document).ready(function () {
	$(".se-pre-con").fadeOut("slow");
	if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i))
		Utils.mobileDeviceFlag = true;
	/* if(Utils.mobileDeviceFlag)
		Utils.Path=Android.getMediaPath("Aksharkhel");  */
	setTimeout(function () {
		Aksharkhel.init();
		if (Aksharkhel.hardCodedData['languageFont'] != null)
			setFontFamilyForLang("fonts/" + Aksharkhel.hardCodedData['languageFont']);
	}, 200);
});

setFontFamilyForLang = function (fontFilePath) {
	var newStyle = "<style>" +
		"@font-face {  " +
		"font-family: 'CustomFontFamily';" +
		"src: url('" + fontFilePath + "') format('truetype');" +
		"}  </style>";
	$("head").append(newStyle);
	$("body").css("font-family", "CustomFontFamily");
}

//runs @onload of body.sets the homepage of game. 
Aksharkhel.init=function()
{
	Aksharkhel.initializeVariables();
	document.getElementById("level1").setAttribute("index",0);
	document.getElementById("level2").setAttribute("index",1);
	document.getElementById("home").style.visibility ="hidden";
	document.getElementById("levelDiv").style.display="none";
	document.getElementById("gamePage").style.display="none";
	document.getElementById("resultDiv").style.display="none";
	//document.getElementById("endpage").style.display="none";
	document.body.style.backgroundImage = "url("+Utils.Path+"images/skin/"+Aksharkhel.hardCodedData.coverImage+")";
	Aksharkhel.setAllImage();
	Aksharkhel.createAcknowledgement();
}

Aksharkhel.initializeVariables = function () {
	Aksharkhel.finalAnsArr = [];
	Aksharkhel.noOfKanaMatra = 0;
	Aksharkhel.wordArray = [];
	Aksharkhel.numOfQuestion = 0;
	Aksharkhel.nextQuestionTimeout;
	Aksharkhel.correct = 0;
	Aksharkhel.wrong = 0;
	Aksharkhel.dataArray = [];
	Aksharkhel.letterCounter = 0;
	Aksharkhel.totalQuestions = 5;
	Aksharkhel.noOflettersInAnswer = 0;
	Aksharkhel.playWordTime = 0;
	Aksharkhel.loadmetaDataFlag = false;
	Aksharkhel.correctFlag = true;
	Aksharkhel.scoreFlag = true;
	Aksharkhel.indexArray = [];
	Aksharkhel.attemptFlag = false;
}

Aksharkhel.createAcknowledgement = function () {
	var i = 0, div;
	$("#acknowledgementDiv").empty();
	console.log(Object.keys(Aksharkhel.AcknowledgementPageData).length)
	for (i = 0; i < Object.keys(Aksharkhel.AcknowledgementPageData).length; i++) {
		div = document.createElement('div');
		div.className = "fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
		div.innerHTML = Object.keys(Aksharkhel.AcknowledgementPageData)[i] + " : " + Object.values(Aksharkhel.AcknowledgementPageData)[i];
		acknowledgementDiv.appendChild(div);
	}
	div = document.createElement('div');
	div.className = "fontClass firstLetter col-xs-12 col-sm-12 col-md-12 col-lg-12";
	div.innerHTML = Aksharkhel.hardCodedData['createdDate'];
	div.style.marginBottom = "2vh";
	acknowledgementDiv.appendChild(div);
}

//called from Aksharkhel.init function,sets sources to all the buttons and images.	
Aksharkhel.setAllImage=function()
{
	document.getElementById("homeLogo").src=Utils.Path+"images/skin/Home_button.png";
	document.getElementById("logo").src=Utils.Path+"images/skin/logo.png";
	document.getElementById("play").src=Utils.Path+"images/skin/playButton.png";
	document.getElementById("nextBtnImg").src=Utils.Path+"images/skin/playButton.png";
	document.getElementById("next").src=Utils.Path+"images/skin/Next_button.png";
	document.getElementById("sound").src=Utils.Path+"images/skin/Sound_button.png";
	document.getElementById("replay").src=Utils.Path+"images/skin/playAgain.png";
	document.getElementById("lev1img").src=Utils.Path+"images/skin/Level_1.png";
	document.getElementById("lev2img").src=Utils.Path+"images/skin/Level_2.png";
	document.getElementById("creditImg").src=Utils.Path+"images/skin/credit.png";
	document.getElementById("imageAndLetter").style.backgroundImage = "url("+Utils.Path+"images/skin/Top_image.png)";
	document.getElementById("outerDiv").style.backgroundImage = "url("+Utils.Path+"images/skin/Bottom_image.png)";
	document.getElementById("letterOuterDiv").style.backgroundImage = "url("+Utils.Path+"images/skin/Image_box.png";
}

//called from next button on homepage.Loads the levelpage.You have to choose which level you are going to play.
Aksharkhel.showLevel=function()
{
	document.getElementById("playButton").style.display="none";
	document.getElementById("home").style.visibility = "visible";
	document.getElementById("levelDiv").style.display="block";
	document.getElementById("gamePage").style.display="none";
	document.getElementById("resultDiv").style.display="none";
	document.body.style.backgroundImage = "url("+Utils.Path+"images/skin/Bg.png)";
}

//runs @ onclick of level. sets the level-index as per level selected.
Aksharkhel.selectLevel=function(myID)
{
	Aksharkhel.levelIndex=document.getElementById(myID).getAttribute('index');
	level=parseInt(Aksharkhel.levelIndex);//score mdhli level
	Aksharkhel.showInstruction();
	Aksharkhel.createOptionDivs();
}

//shows the instuction page
Aksharkhel.showInstruction = function () {
	document.getElementById("instructionDiv").style.display = "block";
	document.getElementById('heading').innerHTML = Aksharkhel.hardCodedData.heading;
	document.getElementById('text').innerHTML = Aksharkhel.hardCodedData.instructions[Aksharkhel.levelIndex ];
}

//resets the gamepage.
Aksharkhel.resetAll=function()
{
	Aksharkhel.splitArray=[];
	Aksharkhel.correct=0;
	Aksharkhel.wrong=0;
	Aksharkhel.numOfQuestion=0;
	Aksharkhel.correctFlag=true;
	document.getElementById("levelDiv").style.display="none";
	document.body.style.backgroundImage = "url("+Utils.Path+"images/skin/Bg.png)";
	document.getElementById("home").style.visibility = "visible";
	document.getElementById("resultDiv").style.display="none";
	document.getElementById("gamePage").style.display="block";
	document.getElementById("scoreSpan").style.visibility="visible";
	document.getElementById("playButton").style.display="none";
	document.getElementById("scoreSpan").innerHTML=Aksharkhel.hardCodedData.marks+Aksharkhel.correct*10;
	document.getElementById("instructionDiv").style.display="none";
	$("#acknowledgementDiv").css("visibility","hidden");
	Aksharkhel.createArray();
	Aksharkhel.playGame();
}

//creates array 5 unique indexes for setting the question images.
Aksharkhel.createArray = function () {
	var i, rNo;
	Aksharkhel.dataArray = [];
	for (i = 0; i < 5; i++) {
		rNo = Math.floor(Math.random() * (Aksharkhel.Data[level].list.length));
		if (Aksharkhel.dataArray.includes(Aksharkhel.Data[level].list[rNo])) {
			i--;
			continue;
		}
		else
			Aksharkhel.dataArray.push(Aksharkhel.Data[level].list[rNo]);
	}
}

//called from Aksharkhel.playGame(),sets all the mulakshare letters to option.
Aksharkhel.createOptionDivs = function () {
	var i, j, optionDiv, span, parentDiv, uniqueArr = [], finalAnsArr = [], totalOptions;
	parentDiv = document.getElementById("options");

	$("#options").empty();
	if (Aksharkhel.levelIndex == 0)
		totalOptions = 11;
	else
		totalOptions = 16;
	for (i = 0; i < totalOptions; i++) {
		optionDiv = document.createElement("DIV")
		if (Aksharkhel.levelIndex == 0)
			if (i == 8) {
				optionDiv.setAttribute("class", "optionDiv col-lg-3 col-md-3 col-sm-3 col-xs-3");
				$(optionDiv).css("margin-left", "3.5vw");
			}
			else
				optionDiv.setAttribute("class", "optionDiv col-lg-3 col-md-3 col-sm-3 col-xs-3");
		else
			if (i == 12)
				optionDiv.setAttribute("class", "optionDiv col-lg-2 col-md-2 col-sm-2 col-xs-2 col-xs-offset-2 col-sm-offset-2 col-md-offset-2 col-lg-offset-2");
			else
				optionDiv.setAttribute("class", "optionDiv col-lg-2 col-md-2 col-sm-2 col-xs-2");
		optionDiv.setAttribute("id", "optionDiv" + i);
		optionDiv.setAttribute("index", i);
		optionDiv.setAttribute("onclick", "Aksharkhel.checkAnswer(this.id)");
		optionDiv.style.backgroundImage = "url(" + Utils.Path + "images/skin/blank_button.png";

		span = document.createElement("SPAN");
		span.setAttribute("class", "optionSpan");
		span.setAttribute("id", "optionSpan" + i);

		parentDiv.appendChild(optionDiv);
		optionDiv.appendChild(span);
	}
}

//runs @ onclick of next button on homepage sets gamepage of game.  
Aksharkhel.playGame = function () {
	var d;
	Aksharkhel.attemptFlag = false;
	clearTimeout(Aksharkhel.playWordTime);
	clearTimeout(Aksharkhel.nextQuestionTimeout);
	Aksharkhel.correctFlag = true;
	Aksharkhel.noOfKanaMatra = 0;
	scoredMarks = 0;
	Aksharkhel.indexArray = [];
	if (Aksharkhel.scoreFlag == false) {
		Aksharkhel.scoreFlag = true;
		console.log(resId, questionId, scoredMarks, totalMarks, level, startTime);
		if (Utils.mobileDeviceFlag)
			Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime);
	}
	d = new Date();
	startTime = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
	Aksharkhel.numOfQuestion++;
	if (Aksharkhel.numOfQuestion > Aksharkhel.totalQuestions) {
		Aksharkhel.endGame();
	}
	else {
		if (Aksharkhel.levelIndex == 0)
			$(".optionDiv").css("pointer-events", "none");
		else
			$(".optionDiv").css("pointer-events", "auto");
		Aksharkhel.letterCounter = 0;
		document.getElementById("options").style.pointerEvents = "auto";
		document.getElementById("soundDiv").style.pointerEvents = "auto";
		document.getElementById("nextDiv").style.visibility = "visible";
		Aksharkhel.setQuestion();
		Aksharkhel.setOptions();
	}
}
//displays the score.
Aksharkhel.endGame = function () {
	document.getElementById("audio").pause();
	document.getElementById("gamePage").style.display = "none";
	document.getElementById("scoreSpan").style.visibility = "hidden";
	document.getElementById("resultDiv").style.display = "block";
	document.getElementById("replay").style.display = "block";
	$(".optionDiv").css("pointer-events", "auto");
	document.getElementById("final").innerHTML = Aksharkhel.hardCodedData.word;
	document.getElementById("score").innerHTML = Aksharkhel.hardCodedData.score + Aksharkhel.correct * 10;
}
//called from Aksharkhel.playgame and  Aksharkhel.reset function.creates dynamic divs for the images
Aksharkhel.setQuestion = function () {
	var word, spanDiv, span, i, j;
	clearTimeout(Aksharkhel.playWordTime);
	document.getElementById("audio").src = Utils.Path + "sounds/" + Aksharkhel.dataArray[Aksharkhel.numOfQuestion - 1].sound;
	document.getElementById("audio").play();
	questionId = Aksharkhel.dataArray[Aksharkhel.numOfQuestion - 1].questionId;
	word = Aksharkhel.dataArray[Aksharkhel.numOfQuestion - 1].word;
	Aksharkhel.wordArray = word.split("");
	for (i = 0; i < Aksharkhel.wordArray.length; i++) {
		if (Aksharkhel.KanaMatraArr.includes(Aksharkhel.wordArray[i])) {
			Aksharkhel.wordArray.splice(i, 1);//word without kanamatra.
			Aksharkhel.noOfKanaMatra++;
		}
	}
	Aksharkhel.splitArray = word.split("");
	for (i = 0; i < Aksharkhel.splitArray.length; i++) {
		if (Aksharkhel.KanaMatraArr.includes(Aksharkhel.splitArray[i])) {
			Aksharkhel.splitArray[i - 1] = Aksharkhel.splitArray[i - 1] + Aksharkhel.splitArray[i];
			if(Aksharkhel.splitArray[i]=="्")
			Aksharkhel.indexMain=[i];
			//Aksharkhel.ansWord=Aksharkhel.splitArray[i];
			Aksharkhel.splitArray.splice(i, 1);//word with kanamatra. 
		}
	}
	Aksharkhel.noOflettersInAnswer = Aksharkhel.splitArray.length;
	spanDiv = document.getElementById("letterDiv");
	$("#letterDiv").empty();
	for (j = 0; j < Aksharkhel.splitArray.length; j++) {
		span = document.createElement("SPAN");
		span.setAttribute("id", "span" + j);
		if (Aksharkhel.levelIndex == 0)
			span.setAttribute("class", "wordSpan1");
		else
			span.setAttribute("class", "wordSpan2");
		if(level==0)
		span.innerHTML = Aksharkhel.wordArray[j];
		else
		span.innerHTML = Aksharkhel.splitArray[j];
		span.setAttribute("mulakshar", Aksharkhel.splitArray[j]);
		if (Aksharkhel.levelIndex == 0)
			span.setAttribute("onclick", "Aksharkhel.getLetter(this.id);");
		
		spanDiv.appendChild(span);
	}
	if (Aksharkhel.levelIndex == 1)
		$("#span" +Aksharkhel.indexMain ).hide();
}
//Calls from Aksharkhel.playgaeme().sets options from barakhadi to the displayed image
Aksharkhel.setOptions = function () {
	var i = 0, j = 0, k = 0, optionDiv = 0, span = 0, parentDiv = 0, uniqueArr = [];
	if (Aksharkhel.levelIndex == 0)
		for (i = 0; i < 11; i++)
			document.getElementById("optionSpan" + i).innerHTML = Aksharkhel.KanaMatraArr[i];
	if (Aksharkhel.levelIndex == 1) {
		uniqueArr = Aksharkhel.splitArray.slice(0);
		Aksharkhel.removeDuplicate(uniqueArr);
		Aksharkhel.finalAnsArr = uniqueArr.slice(0);
		Aksharkhel.getLetterIndex();
		Aksharkhel.getOption();

		Aksharkhel.shuffle(Aksharkhel.finalAnsArr);
		for (i = 0; i < 16; i++) {
			document.getElementById("optionSpan" + i).innerHTML = Aksharkhel.finalAnsArr[i];
		}
	}
}
//Fetch the the indexes of letters present in the word(question) from mulakshare. 
Aksharkhel.getLetterIndex = function () {
	var i = 0, j = 0, a;
	for (i = 0; i < Aksharkhel.wordArray.length; i++) {
		a = $.inArray(Aksharkhel.wordArray[i], Aksharkhel.mulAkshare);
		if (a != -1)
			Aksharkhel.indexArray.push(a);
	}
}
//gets the random option from the barakhadi of letters present in the word(question).
Aksharkhel.getOption = function () {
	var j = 0, length = Aksharkhel.indexArray.length;
	letterCnt = Array(length).fill(1);
	for (j = Aksharkhel.finalAnsArr.length; j < 16; j++) {
		randomIndex = Math.floor(Math.random() * Aksharkhel.wordArray.length);
		while (!(letterCnt[randomIndex] < 11))
			randomIndex = Math.floor(Math.random() * Aksharkhel.wordArray.length);
		randomletter = Math.floor(Math.random() * Aksharkhel.wordsData[Aksharkhel.indexArray[randomIndex]]['list'].length);
		while ($.inArray(Aksharkhel.wordsData[Aksharkhel.indexArray[randomIndex]]['list'][randomletter]['word'], Aksharkhel.finalAnsArr) != -1)//Aksharkhel.finalAnsArr.includes(Aksharkhel.wordsData[k]['list'][randomletter]['word']))
		{
			randomletter = Math.floor(Math.random() * Aksharkhel.wordsData[Aksharkhel.indexArray[randomIndex]]['list'].length);
		}
		Aksharkhel.finalAnsArr.push(Aksharkhel.wordsData[Aksharkhel.indexArray[randomIndex]]['list'][randomletter]['word']);
		letterCnt[randomIndex] = letterCnt[randomIndex] + 1;
	}
}
//Runs @ onclick of letter displayed on typewriter.stores the clicked letter.
Aksharkhel.getLetter = function (letter) {
	Aksharkhel.letterDiv = document.getElementById(letter);
	$(".wordSpan1").css("color", "black");
	Aksharkhel.letterDiv.style.color = "#d42a78";
	$(".optionDiv").css("pointer-events", "auto");
}
//Runs @ onclick of letter from option checks whether the initial letter of image and cliked letter are same.
Aksharkhel.checkAnswer = function (letterId) {
	Aksharkhel.attemptFlag = true;
	Aksharkhel.scoreFlag = false;
	if (Aksharkhel.levelIndex == 0)
		Aksharkhel.checkAnsForLevel1(letterId);
	else
		Aksharkhel.checkAnsForLevel2(letterId);
}
//for level 1
//runs @onclick of letter from option. checks whether the clicked letter and initial letter of image displayed are same.
Aksharkhel.checkAnsForLevel1 = function (letterId) {
	var indexArray = [], index, playWordTime, sound1, sound2, letter;
	letter = Aksharkhel.letterDiv.innerHTML + document.getElementById(letterId).children[0].innerHTML;
	if (Aksharkhel.letterDiv.getAttribute("mulakshar") == letter) {
		for (i = 0; i < Aksharkhel.splitArray.length; i++) {
			if (letter == Aksharkhel.splitArray[i]) {
				document.getElementById("span" + i).innerHTML = Aksharkhel.splitArray[i];
				document.getElementById(letterId).style.color = "green";
				Aksharkhel.letterDiv.style.opacity = 1;
				Aksharkhel.letterDiv.style.pointerEvents = "none";
				Aksharkhel.checkWordAndPlaySound(Aksharkhel.splitArray[i], i);
				$(".optionDiv").css("pointer-events", "none");
				setTimeout(function () {
					document.getElementById(letterId).style.color = "black";
				}, 1000);
				break;
			}
		}
	}
	else {
		Aksharkhel.correctFlag = false;
		scoredMarks = 0;
		document.getElementById(letterId).style.color = "red";
		Aksharkhel.wrong++;
		$(".optionDiv").css("pointer-events", "none");
		setTimeout(function () {
			document.getElementById(letterId).style.color = "black";
			$(".optionDiv").css("pointer-events", "auto");
		}, 1000);
		document.getElementById("audio").src = Utils.Path + "sounds/wrongSoundAksharkhadi.mp3";
		document.getElementById("audio").play();
	}
}
//for level 2.
//runs @onclick of letter from option. checks whether the clicked letter and letters of image displayed are same.
Aksharkhel.checkAnsForLevel2 = function (letterId) {
	var letter = "", i;
	letter = document.getElementById(letterId).children[0].innerHTML;
	/* if (Aksharkhel.splitArray.includes(letter)) {
		for (i = 0; i < Aksharkhel.splitArray.length; i++) { */
			if (letter == Aksharkhel.splitArray[Aksharkhel.indexMain]) {
				//document.getElementById("span" + i).innerHTML = letter;
				document.getElementById("span" + [Aksharkhel.indexMain-1]).innerHTML=document.getElementById("span" + [Aksharkhel.indexMain-1]).innerHTML+letter
				//document.getElementById("span" + i).style.visibility = "visible";
				document.getElementById(letterId).style.color = "green";
				Aksharkhel.checkWordAndPlaySound(letter, Aksharkhel.indexMain);
				$(".optionDiv").css("pointer-events", "none");
				setTimeout(function () {
					document.getElementById(letterId).style.color = "black";
					/* if (Aksharkhel.letterCounter < Aksharkhel.noOflettersInAnswer)
						$(".optionDiv").css("pointer-events", "auto"); */
				}, 1000);
			//	break;
	/* 		}
		} */
	}
	else {
		Aksharkhel.correctFlag = false;
		scoredMarks = 0;
		document.getElementById(letterId).style.color = "red";
		$(".optionDiv").css("pointer-events", "none");
		setTimeout(function () {
			document.getElementById(letterId).style.color = "black";
			$(".optionDiv").css("pointer-events", "auto");
		}, 1000);
		document.getElementById("audio").src = Utils.Path + "sounds/wrongSoundAksharkhadi.mp3";
		document.getElementById("audio").play();
	}
}
//get duration function gets the duration of sound and returns the duration to playWordSound() function. 
Aksharkhel.getDuration = function (sound1, sound2) {
	var duration = 0;
	$('#audio').prop('src', sound1);
	if (Aksharkhel.loadmetaDataFlag) {
		$('#audio').on("loadedmetadata", function () {
			Aksharkhel.loadmetaDataFlag = false;
			$('#audio').unbind("loadedmetadata");
			duration = $('#audio').prop('duration');

			if (duration == 0) {
				duration = 1;
				console.log(duration);
			}
			Aksharkhel.playWordSound(duration, sound2);
		}
		);
	}
	else
		$('#audio').unbind("loadedmetadata");
}
//plays the sound of letter in timeout. 
Aksharkhel.playWordSound = function (duration, sound2) {
	var src;
	document.getElementById("audio").play();
	if (Aksharkhel.playWordTime) {
		clearTimeout(Aksharkhel.playWordTime);
		Aksharkhel.playWordTime = null;
	}
	Aksharkhel.playWordTime = setTimeout(function () {
		document.getElementById("audio").src = sound2;
		document.getElementById("audio").play();
	}, duration * 1100);
}
//plays sound of letter clicked from option if correct.
Aksharkhel.checkWordAndPlaySound = function (word, index) {
	var splitWord = [], spanIndex = 0, playWordTime, source, i, j;
	splitWord = word.split("");
	spanIndex = Aksharkhel.splitArray.indexOf(word);
	document.getElementById("span" + spanIndex).style.color = "rgba(0,0,0,1)";
	Aksharkhel.letterCounter++;
	Aksharkhel.splitArray.splice(index, 1, 0);
	//Aksharkhel.splitArray.splice(index,0,0);
	for (i = 0; i < Aksharkhel.wordsData.length; i++) {
		if (Aksharkhel.wordsData[i].suffix == splitWord[0]) {
			for (j = 0; j < Aksharkhel.wordsData[i].list.length; j++) {
				if (Aksharkhel.wordsData[i].list[j].word == word) {
					if ((Aksharkhel.levelIndex == 0 && Aksharkhel.letterCounter == Aksharkhel.noOfKanaMatra) || (Aksharkhel.levelIndex == 1)) {
						if (Aksharkhel.correctFlag == true) {
							scoredMarks = 10;
							Aksharkhel.correct++;
							document.getElementById("scoreSpan").innerHTML = Aksharkhel.hardCodedData.marks + Aksharkhel.correct * 10;
							/* Aksharkhel.nextQuestionTimeout = setTimeout(function () {
								$(".optionDiv").css("pointerEvents", "none");
								Aksharkhel.playGame();
							}, 3000); */
						}
						else
							scoredMarks = 0;
						$(".optionDiv").css("pointerEvents", "none");
						source1 = Utils.Path + "sounds/" + Aksharkhel.wordsData[i].list[j].sound;
						source2 = Utils.Path + "sounds/" + Aksharkhel.dataArray[Aksharkhel.numOfQuestion - 1].sound;
						Aksharkhel.loadmetaDataFlag = true;
						Aksharkhel.getDuration(source1, source2);
					}
					else {
						document.getElementById("audio").src = Utils.Path + "sounds/" + Aksharkhel.wordsData[i].list[j].sound;
						document.getElementById("audio").play();
						if (Aksharkhel.nextQuestionTimeout) {
							clearTimeout(Aksharkhel.nextQuestionTimeout);
							Aksharkhel.nextQuestionTimeout = null;
						}
					
					}
					break;
				}
			}
			break;
		}
	}
	if ((Aksharkhel.levelIndex == 0 && Aksharkhel.letterCounter == Aksharkhel.noOfKanaMatra) || (Aksharkhel.levelIndex == 1)) {
		Aksharkhel.scoreFlag = true;
		$(".wordSpan1").css("pointer-events", "none");
		if (Aksharkhel.nextQuestionTimeout) {
			clearTimeout(Aksharkhel.nextQuestionTimeout);
			Aksharkhel.nextQuestionTimeout = null;
		}
		Aksharkhel.nextQuestionTimeout = setTimeout(function () {
			Aksharkhel.playGame();
		}, 3000);

		console.log(resId, questionId, scoredMarks, totalMarks, level, startTime);
		if (Utils.mobileDeviceFlag)
			Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime);
	}
};
//runs @ onclick of sound button, plays the sound of image. 	
Aksharkhel.playSound = function () {
	document.getElementById("audio").src = Utils.Path + "sounds/" + Aksharkhel.dataArray[Aksharkhel.numOfQuestion - 1].sound;
	document.getElementById("audio").play();

}
//reloads the webpage.
Aksharkhel.callHome=function()
{
	clearTimeout(Aksharkhel.nextQuestionTimeout);
	document.getElementById("audio").pause();
	clearTimeout(Aksharkhel.playWordTime);
	if(document.getElementById("levelDiv").style.display=="block")	
		location.reload(true);
	else
	{
		Aksharkhel.showLevel();
		document.getElementById("scoreSpan").style.visibility="hidden";
		if(Aksharkhel.scoreFlag==false)
		{
			Aksharkhel.scoreFlag=true;
			console.log(resId,questionId,scoredMarks,totalMarks,level,startTime);
			if(Utils.mobileDeviceFlag)
				Android.addScore(resId,questionId,scoredMarks,totalMarks,level,startTime);	
		}
	}
}
//for making array unique
Aksharkhel.removeDuplicate = function (array) {
	var i = 0, j = 0;
	for (i = 1; i < array.length; i++) {
		for (j = 0; j < i; j++) {
			if (array[i] == array[j]) {
				array.splice(i, 1);
			}
		}
	}
	return;
}
//shuffle logic
Aksharkhel.shuffle = function (array) {
	var ctr = 0, temp = 0, index = 0;
	for (ctr = 0; ctr < Aksharkhel.splitArray.length; ctr++) {
		index = Math.floor(Math.random() * array.length);
		temp = array[ctr];
		array[ctr] = array[index];
		array[index] = temp;
	}
	return array;
}
//runs when user clicks next button wijthout attempting question.
Aksharkhel.showNextQuestion = function () {
	if (Aksharkhel.attemptFlag == false) {
		scoredMarks = -2;
		console.log(resId, questionId, scoredMarks, totalMarks, level, startTime);
		if (Utils.mobileDeviceFlag)
			Android.addScore(resId, questionId, scoredMarks, totalMarks, level, startTime);
	}
	Aksharkhel.playGame();
}

//displays acknowledgement.
Aksharkhel.showAcknowlwdgement = function () {
	$("#acknowledgementDiv").css("visibility", "visible");
}