
function calcage(secs, num1, num2) {
    s = ((Math.floor(secs/num1))%num2).toString();
    if (LeadingZero && s.length < 2)
        s = "0" + s;
    return "<b>" + s + "</b>";
}

function timerEnd() {
    document.getElementById("timer").innerHTML = FinishMessage;
    var answers = testCenterObj.answers;
    //alert(answers.length);
    var tempAnswers = [];

    //alert("Num of ques = "+questions.numOfQuestions);
    for(i = 1; i <= questions.numOfQuestions; i++) {
        tempAnswers[questions.questions[i]['questionId']] = [];
        tempAnswers[questions.questions[i]['questionId']]['option'] = '';
        tempAnswers[questions.questions[i]['questionId']]['timeTaken'] = 0;
    }
    for(i in answers) {
        //alert("IN Answers");
        if((typeof(i) == "string")&&(i!="undefined")&&(i!="include")) {
            tempAnswers[questions.questions[i]['questionId']]['option'] = answers[i];
            tempAnswers[questions.questions[i]['questionId']]['timeTaken'] = 5;
            //alert("Question Attempted "+i)
        }
    }
    alert("You Time is Over. Submitting Test!!");
    testCenterObj.completeTest();
    //#TODO Need to Submit Test

}

function CountBack() {
    secs = gsecs;
    DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
    DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
    DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
    DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));

    document.getElementById("timer").innerHTML = DisplayStr;
    gsecs = secs+CountStepper;
    if(typeof(testCenterObj) != "undefined") {
        if(testCenterObj.quesTime[testCenterObj.currentQuestion]== undefined)
        {
            testCenterObj.quesTime[testCenterObj.currentQuestion] = 1;
        }else{
            testCenterObj.quesTime[testCenterObj.currentQuestion]++;
        }
    }
}

function timerInitialize() {
if (typeof(TargetDate)=="undefined")
    TargetDate = new Date().valueOf() + 2*3600000 ;
else
    TargetDate = new Date().valueOf() + TargetDate;
if (typeof(DisplayFormat)=="undefined")
    DisplayFormat = "%%H%%:%%M%%:%%S%%";
if (typeof(CountActive)=="undefined")
    CountActive = true;
if (typeof(FinishMessage)=="undefined")
    FinishMessage = "Time Up";
if (typeof(CountStepper)!="number")
    CountStepper = -1;
if (typeof(LeadingZero)=="undefined")
    LeadingZero = true;

CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
    CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
var dthen = new Date(TargetDate);
var dnow = new Date();
if(CountStepper>0)
  ddiff = new Date(dnow-dthen);
else
  ddiff = new Date(dthen-dnow);

gsecs = Math.floor(ddiff.valueOf()/1000);
}
