var iFinishedQ=0;
var bStarted=0;
var zz;
var sMessage="";//"Click on \"Start\" to answer the questions. Use &lt;Tab&gt; or &lt;Enter&gt; to go to the next question.<br>Click \"Stop\" after completing all the questions. Questions that will take you the most time will be highlighted in Blue color.";
for (zz=1; zz<dans.length; zz++)
{
	aiTime[zz]=0;
	aiResult[zz]=0;
}

var dQTime1 = null;
var TimerQ=null;
var iTotal=0;
var iScore=0;
var iCurrTIndex=0;
document.onkeydown =
			function (evt) {
				var alt=false;
				var ctrl=false;
				var shift=false;
				var keyCode;
				try{
				if (!window.event){
					//NS 6
					//window.alert(evt.ctrlKey);
					//mykey = evt.which;
					alt = evt.altKey;//(evt.modifiers & Event.ALT_MASK) ? true : false;
					ctrl = evt.ctrlKey;//(evt.modifiers & Event.CONTROL_MASK) ? true : false;
					shift = evt.shiftKey;//(evt.modifiers & Event.SHIFT_MASK) ? true : false;
				} else {
					//IE, Opera
					//window.alert(evt.modifiers);
					//window.alert(evt.shiftKey);
					//mykey = evt.keyCode;
					alt = event.altKey;
					ctrl = event.ctrlKey;
					shift = event.shiftKey;
				}
				}
				catch(err)
				{
				}
				keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
				if (alt==false && ctrl==false && shift==false && (keyCode == 13 || keyCode == 9)) 
				{
					//window.alert(iCurrTIndex);
					if (iCurrTIndex>0 && iCurrTIndex<=iQMax)
					{
						//aTexts[iCurrTIndex+1].disabled='true';
						if (iCurrTIndex>=iQMax)
						{
							CheckOut(iQMax);
							document.getElementById("TName").focus();
							//setTimeout(function() { myFunc(a, b, c); }, ms);
							//setTimeout(StopW(), 1000);
							//window.alert("==>"+aiResult[50]);
							StopW();
							return false;
							iCurrTIndex=0;
						}
						aTexts[iCurrTIndex+1].disabled='';
						aTexts[iCurrTIndex+1].focus();
						return false;
					}
					else
						return true;
				}
				else
				{
					return true;
				}
			};


function DisplayHelp2(oParent)
{
	//document.getElementById("idMessage").innerHTML=sMessage;
	var elem=document.getElementById("idPopupHelp2");
	//elem.innerHTML=sMessage;

		//Get location of mouseover object, move help object to the left if will display off-screen
		var divWidth = 340;
		var offsetLeft = getOffsetLeft(oParent);
		var screenWidth = (window.innerWidth) ? window.innerWidth - 25 : document.body.clientWidth;
		if ((offsetLeft + divWidth) > screenWidth) offsetLeft = screenWidth - divWidth;
		var newX = offsetLeft;
	
		//Get location of mouseover object, move help object on top if will display off-screen
		var divHeight = elem.offsetHeight;
		var offsetTop = getOffsetTop(oParent) + oParent.offsetHeight;
		var screenHeight = (window.innerHeight) ? window.innerHeight - 25 : document.body.clientHeight;
		if ((offsetTop + divHeight) > screenHeight + getScrollY()) offsetTop = getOffsetTop(oParent) - divHeight;
		var newY = offsetTop;	

	//window.alert(newY+":"+newX);
	elem.style.top = newY;
	elem.style.left = newX;
	elem.style.visibility = 'visible';
	
}

function DisplayHelp(oParent)
{
	//document.getElementById("idMessage").innerHTML=sMessage;
	var elem=document.getElementById("idPopupHelp");
	//elem.innerHTML=sMessage;

		//Get location of mouseover object, move help object to the left if will display off-screen
		var divWidth = 340;
		var offsetLeft = getOffsetLeft(oParent);
		var screenWidth = (window.innerWidth) ? window.innerWidth - 25 : document.body.clientWidth;
		if ((offsetLeft + divWidth) > screenWidth) offsetLeft = screenWidth - divWidth;
		var newX = offsetLeft;
	
		//Get location of mouseover object, move help object on top if will display off-screen
		var divHeight = elem.offsetHeight;
		var offsetTop = getOffsetTop(oParent) + oParent.offsetHeight;
		var screenHeight = (window.innerHeight) ? window.innerHeight - 25 : document.body.clientHeight;
		if ((offsetTop + divHeight) > screenHeight + getScrollY()) offsetTop = getOffsetTop(oParent) - divHeight;
		var newY = offsetTop;	

	//window.alert(newY+":"+newX);
	elem.style.top = newY;
	elem.style.left = newX;
	elem.style.visibility = 'visible';
	
}

function HideHelp(oParent)
{
	//document.getElementById("idMessage").innerHTML="";
	var elem=document.getElementById("idPopupHelp");
	elem.style.visibility = 'hidden';
}

function HideHelp2()
{
	//document.getElementById("idMessage").innerHTML="";
	var elem=document.getElementById("idPopupHelp2");
	elem.style.visibility = 'hidden';
}

function StopW()
{
	//window.alert(		bStarted+":"+iTotal+":"+iScore);
	if (bStarted==0)
		return;
	iScore=iTotal;
	try{
		var iAvg=0;
		var z;
		iFinishedQ=0;
		var iMin=32767;
		var iMax=0;
		var sTotalTime="";
		var iHighlightCount=0;
		//var sTest="";
		if (TimerQ!=null)
		{
			window.clearTimeout(TimerQ);
			TimerQ=null;
		}
		sTotalTime=GetDescriptiveTime(iTotal);
		document.getElementById("divTime").innerHTML=sTotalTime;
		iTotal=0;
		document.getElementById("idStopTimer").style.display="none";
		document.getElementById("idStartTimer").style.display="inline";
		//window.alert(aiTime.length);
		//window.alert(dans.length+"::"+aiTime.length+"::"+aiResult.length+"::"+aTexts.length);
		//var sTest="";
		var iCompleted=0;
		for (z=1; z<aiTime.length; z++)
		{
			//sTest=sTest+"["+z+"]: "+aiResult[z]+" ";
			//if (z<5)
			//	window.alert(aiTime[z]+":"+iFinishedQ);
			//window.alert(z+"::"+aiTime[z]+"::"+aiResult[z]+"::"+iAvg);
			if (isNaN(aiTime[z]))
			{	
				//window.alert("A:"+z+"::"+aiTime[z]+"::"+aiResult[z]+"::"+iAvg);
			}
			else if (aiResult[z]==0)
			{
				//window.alert("B:"+z+"::"+aiTime[z]+"::"+aiResult[z]+"::"+iAvg);
			//aiTime[z]<250
			}
			else
			{				
				if (aiResult[z]==1)
					iCompleted++;
				//window.alert(aTexts[z].value+":"+aiTime[z]);
				if (z==1)
					aiTime[z]=aiTime[z]/2;
				iFinishedQ++;
				iAvg=iAvg+aiTime[z];
				if (iMax<aiTime[z])
					iMax=aiTime[z];
				if (iMin>aiTime[z])
					iMin=aiTime[z];
				//sTest=sTest+z+":"+aiTime[z]+" ";
			}	
		}
		iAvg=iAvg/iFinishedQ;
		iAvg=(iMax+iAvg)/2;
		//window.alert(aiTime.length+"::"+aiResult.length+"::"+aTexts.length);
		//window.alert(50+"::"+aiTime[50]+"::"+aiResult[50]);
		for (z=1; z<aiTime.length; z++)
		{
			//if (z==50)
			//window.alert(z+"::"+aiTime[z]+"::"+aiResult[z]+"::"+iAvg);
			if (aiResult[z]==1)
			{
				if (aiTime[z]>iAvg)
				{
					var elemSelect=aTexts[z];
					if (elemSelect.value.length>0)
					{
						elemSelect.style.backgroundColor='#000099';
						elemSelect.style.color='white';
						iHighlightCount++;
					}
				}
			}
			aiTime[z]=0;	
			aiResult[z]=0;
		}
		if (iHighlightCount>0)
			window.alert(iFinishedQ + " Questions completed in "+sTotalTime+"."+"\r\n"+"Questions that took you the most time (i.e more than " + parseInt(iAvg/100)/10 + " seconds) are highlighted in dark Blue color");
		else
			window.alert(iFinishedQ + " Questions completed in "+sTotalTime+".");
		//window.alert(iMin + ":" + iMax + ":" + iAvg+":"+iFinishedQ);
		if (iCompleted==iQMax && iFinishedQ==iQMax && document.getElementById("idScore"))
		{
			document.getElementById("idScore").style.visibility="visible";
			document.getElementById("idDisplayArea").style.display="inline";
			document.getElementById("idScoreMessageArea").innerHTML="";
			document.getElementById("E1").focus();
		}
	}
	catch(err)
	{
		//window.alert(err);
	}
	bStarted=0;
}

function StartW()
{	
	//window.alert(dans.length);
	try{
		if (iTotal==0)
		{
			var z2=0;
			bStarted=1;
			dQTime1=null;
			document.getElementById("idStartTimer").style.display="none";
			document.getElementById("idStopTimer").style.display="inline";
			document.getElementById("divTime").innerHTML="______________";
			document.getElementById("divTime2").innerHTML="Go";
			iCurrTIndex=1;
			for (z2=1; z2<dans.length; z2++)
			{
				//window.alert(dans.length);
				var tTemp2;
				tTemp2=aTexts[z2];
				tTemp2.value="";
				if (z2==1)
				{
					tTemp2.style.backgroundColor='#FFFFFF';
					tTemp2.style.color='black';
					tTemp2.disabled='';
				}
				else
				{
					tTemp2.style.backgroundColor='#E0E0E0';
					tTemp2.style.color='black';
					tTemp2.disabled='true';
				}
				aiTime[z2]=0;
			}
			aTexts[1].focus();
		}
		//document.getElementById("idClickAnswerImage").style.display="none";
		//document.getElementById("txtAns").focus();
		if (iTotal>0)
			document.getElementById("divTime2").innerHTML=GetDescriptiveTime(iTotal);
		iTotal=iTotal+1;
		TimerQ=setTimeout("StartW()", 1000);
	}
	catch(err)
	{
		//window.alert(err);
	}
}

function CheckIn(i)
{
	var t;
	iCurrTIndex=i;
	t=aTexts[i];
	if (i==1)
		DisplayHelp2(t);

	t.style.backgroundColor='#FFFFD8';
	t.style.color='black';
	t.select();
	dQTime1 = new Date();
	return true;
}

function CheckOut(i)
{
	var t
	var dQTime2 = null;
	var iSpan;
	
	HideHelp2();

	//window.alert("OK1");
	dQTime2 = new Date();
	if (dQTime1!=null)
	{
		iSpan= dQTime2-dQTime1;

		if (isNaN(aiTime[i]))
		{
			aiTime[i]=iSpan;	
		}
		else
			aiTime[i]=aiTime[i]+iSpan;
		//window.alert(iSpan);
	}

	t=aTexts[i];
	if (t.value.length>0)
	{
		if (t.value==dans[i])
		{
			t.style.backgroundColor='#CCFFCC';
			aiResult[i]=1;
		}
		else
		{
			t.style.backgroundColor='#FF8C8C';	
			aiResult[i]=-1;
		}
	}
	else
		t.style.backgroundColor='#FFFFFF';
	//if (i==50)
	//	window.alert(i+"::"+aiResult[i]+"::"+t.value+"::"+dans[i]);	
	return true;
}

