// JavaScript Document
// Mr ThanhNT _ Sep 27, 2004

var ID_OutOfTime = "You are out of time";

var intAssessmentTimerID = 0;
var intAssessmentSeconds = 0;
var intAssessmentCounter = 0;
var tAssessmentStart = null;

var blnIsAssessmentTimeout = false;

function resetAssessmentTimer()
{
	intAssessmentTimerID = 0;
	intAssessmentSeconds = 0;
	intAssessmentCounter = 0;
	tAssessmentStart = null;
	blnIsAssessmentTimeout = false;
}

function startAssessmentTimer(intSeconds)
{
	resetAssessmentTimer();
	intAssessmentSeconds = intSeconds;
	tAssessmentStart = new Date();	
	intAssessmentTimerID  = window.setTimeout("updateAssessmentTimer()", 1000);
}

function stopAssessmentTimer()
{
	if(intAssessmentTimerID) {
		clearTimeout(intAssessmentTimerID);
		intAssessmentTimerID  = 0;
	}

	resetAssessmentTimer();
}

function updateAssessmentTimer()
{
	var intRemainingTime;
	var strTemp;
	
	if(intAssessmentTimerID)
		clearTimeout(intAssessmentTimerID);

	if(!tAssessmentStart)
		tAssessmentStart = new Date();

	if (intAssessmentCounter >= intAssessmentSeconds)
	{
		if (blnIsAssessmentTimeout == false)
		{
			alert(ID_OutOfTime);
			blnIsAssessmentTimeout = true;
		}
				
		// reserve for the question being single response
		frames.CenterPage.Done();
		//jump to the next question
		if (frames.CenterPage.nextButton)	
			frames.CenterPage.nextButton.click();
	}
	else
	{
		intAssessmentCounter++;
		
		//display countdown
		intRemainingTime = intAssessmentSeconds - intAssessmentCounter;		
		if (intRemainingTime < 0)
			intRemainingTime = 0;
		strTemp = "Remaining Time : " + second2HourString(intRemainingTime);
		
		// Mr TNT _ March 3rd,2005
		
		if (top.frames.DisplayFrame.BottomFrame.BottomControl1)
		{
			top.frames.DisplayFrame.BottomFrame.BottomControl1.ID_AssessmentTimer.innerText = strTemp;
		}

		/*
		if (top.frames.DisplayFrame.frames.CenterPage.countDown)
		{
			top.frames.DisplayFrame.frames.CenterPage.countDown.style.visibility="visible";		
			frames.CenterPage.countDown.innerText = strTemp;
		}
		*/
	}
	
	intAssessmentTimerID = setTimeout("updateAssessmentTimer()",1000);
}

function second2HourString(intSecond)
{
	var strResult;
	var strTemp;
	var intNum;
	
	strResult = ""
	
	// hour
	intNum = Math.round(intSecond/3600 - 0.5);
	intSecond = intSecond - intNum*3600;
	strTemp = intNum.toString();
	strResult = strResult + strTemp;

	// minute
	intNum = Math.round(intSecond/60 - 0.5);
	intSecond = intSecond - intNum*60;
	strTemp = intNum.toString();
	strResult = strResult + " : " + strTemp;
	
	// second
	strTemp = intSecond.toString();
	strResult = strResult + " : " + strTemp;
	
	return(strResult);
}