/*
  Ref        Date        Who    Desc
  ---        ----        ---    ----
  Error 44   18/06/2004  PG     Time warning was causing Javascript error in IE4
             05/07/2004  MV     Combine finish.js and status.js
                                Amend time warning for NN/IE5/IE4
             12/07/2004  MV     Only fire endTest once
             13/07/2004  MV     Don't show warnings at zero
P085_PC0043  09/05/2005  PG    javascript:
  P085_PC0039  12/05/2005        MV      New options for ufi
  P085_PC0039  10/06/2005        MV      Access Denied error on Move To
               28/07/2005 MV    Round minutes up for ufi

*/


var time = 0;
var timeRemainingInTest = 0;
var TIMER;
var endTestFired = false;


function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if(endstr == -1) {
    endstr = document.cookie.length;
  }
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) {
      return getCookieVal (j);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) {
      break;
    }
  }
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}

function setTime(initTime)
{
  time = GetCookie('username');

  if (time == null) {
    time = 0;
    pathname = location.pathname;
    myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
// set expiry date to 1 year from now.
    var largeExpDate = new Date ();
    largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));
    SetCookie('username',time);
  }
  timeRemainingInTest = initTime;
  
  recordTime();
  initForm();
}

function recordTime()
{
  time++;
  setUpKeyPress();
  pathname = location.pathname;
  myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
// set expiry date to 1 year from now.
  var largeExpDate = new Date ();
  largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));
  SetCookie('username',time);

  if(document.forms['QuestionForm']) {
    document.QuestionForm.TIME.value = time;
  }
  if(document.forms['finishTestForm']) {
    document.finishTestForm.TIMEREVIEW.value = time;
  }
  if(document.forms['statusform']) {
    document.statusform.TIMEREVIEW.value = time;
  }
  if(document.forms['testInfoform']) {
    document.testInfoform.TIMEREVIEW.value = time;
  }	

  var timeleft = timeRemainingInTest - time;
  var hours = timeleft / 3600;
  var minutes = timeleft % 3600 / 60;
  var seconds = (timeleft % 3600) %  60;
  var totalminutes = parseInt(parseInt(hours) * 60) + parseInt(minutes);
  if(isNaN(totalminutes)) {
    totalminutes = 0;
  }
  else {
    totalminutes = parseInt(totalminutes);
  }
  
// Added for Netscape MV 14/6/02

  if(document.layers&&document.confirmfinish) {
    d = document.confirmfinish.document.DisplayForm;
  } 
  else {
    d = document.DisplayForm;
  }

  if(isNaN(parseInt(hours))) {
    d.DISPLAYHOURS.value = 0;
  }
  else {
    d.DISPLAYHOURS.value = parseInt(hours);
  }

  if(isNaN(parseInt(minutes))) {
    d.DISPLAYMINUTES.value = 0;
  }
  else {
    d.DISPLAYMINUTES.value = parseInt(minutes);
  }
  d.DISPLAYSECONDS.value = parseInt(seconds);

  if(document.getElementById) {
    if(document.getElementById('TimeFormTime')) {
      if(totalminutes!=0) {
        document.getElementById('TimeFormTime').firstChild.nodeValue = (totalminutes+1) + ' minute' + (((totalminutes+1)==1)?'':'s');
      }
      else {  
        document.getElementById('TimeFormTime').firstChild.nodeValue = d.DISPLAYSECONDS.value + ' second' + ((d.DISPLAYSECONDS.value==1)?'':'s');
      } 
    }
    if(document.getElementById('TimeWarn1Time')) {
      document.getElementById('TimeWarn1Time').firstChild.nodeValue = totalminutes + ' min '+d.DISPLAYSECONDS.value+' sec';
    }
    if(document.getElementById('TimeWarn2Time')) {
      document.getElementById('TimeWarn2Time').firstChild.nodeValue = totalminutes + ' min '+d.DISPLAYSECONDS.value+' sec';
    }
    if(document.getElementById('TimeWarn3Time')) {
      document.getElementById('TimeWarn3Time').firstChild.nodeValue = totalminutes + ' min '+d.DISPLAYSECONDS.value+' sec';
    }
  }
  if(document.forms['QuestionForm']) {
    var displayTime = parseInt(document.QuestionForm.WARNINGTIME.value);
    var displayTime2 = parseInt(document.QuestionForm.WARNINGTIME2.value);
    var displayTime3 = parseInt(document.QuestionForm.WARNINGTIME3.value);
    var displayFor = parseInt(document.QuestionForm.WARNINGTIMEFOR.value);
    
    if(displayTime>0&&(timeleft <= displayTime && timeleft >= (displayTime-displayFor)) && document.QuestionForm.QUESTION_TIMED.value != "TRUE") {
        showWarningMessage(1);
    }
    if(displayTime2>0&&(timeleft <= displayTime2 && timeleft >= (displayTime2-displayFor)) && document.QuestionForm.QUESTION_TIMED.value != "TRUE") {
        showWarningMessage(2);
    }
    if(displayTime3>0&&(timeleft <= displayTime3 && timeleft >= (displayTime3-displayFor)) && document.QuestionForm.QUESTION_TIMED.value != "TRUE") {
        showWarningMessage(3);
    }
    if(timeleft == (displayTime-displayFor) || timeleft == (displayTime2-displayFor) || timeleft == (displayTime3-displayFor)) {
        showWarningMessage(0);
    }
  }
  clearTimeout(TIMER);
  TIMER = setTimeout("recordTime()",1000);

  if(timeleft <= 0) {
    if(document.forms['QuestionForm']) {
      if((document.QuestionForm.QUESTION_TIMED.value == "TRUE") && (document.QuestionForm.LAST_QUESTION.value == "FALSE")) {
        onNextQuestion();
      }
      else {
        if(!endTestFired) {
          endTest();
          endTestFired=true;
        }
      }
    }
    else {
      if(!endTestFired) {
        endTest();
        endTestFired=true;
      }
    }
  }
}

function endTest()
{
  clearTimeout(TIMER);
  time = 0;
  SetCookie('username',time);    

  if(document.forms['QuestionForm']) { 
    document.QuestionForm.TESTENDED.value = "TRUE";
    document.QuestionForm.SUBMITTED.value = "TRUE";
    document.QuestionForm.submit();
  }
  if(document.forms['finishTestForm']) { 
    document.finishTestForm.TESTENDED.value="TRUE";
    if(document.all) {
      document.all("confirmfinish").style.visibility = "hidden"
      document.all("savingtest").style.visibility = "visible"
    }
    if(document.layers) {
      document.confirmfinish.visibility = "hidden"
      document.savingtest.visibility = "visible"
    }
    document.finishTestForm.submit();
  }
  if(document.forms['testInfoform']) { 
    document.testInfoform.QUESTIONNO.value=1;
    document.testInfoform.TESTENDED.value = "TRUE";
    document.testInfoform.submit();
  }
  return false;
}
function onFinishTest() {
  if(numberAnswersSelected()){
    document.QuestionForm.FINISH.value = "TRUE";
    document.QuestionForm.SUBMITTED.value = "TRUE";
    clearTimeout(TIMER);
    time = 0;
    SetCookie('username',time);    
    document.QuestionForm.submit();
  }  
  return false;
}

function onNextQuestion() {
  if(numberAnswersSelected()){
    alert(Integer.parseInt(document.QuestionForm.QUESTIONNO.value))
    onNextQuestion(Integer.parseInt(document.QuestionForm.QUESTIONNO.value)++)
  }  
  return false;
}

function onPreviousQuestion() {
  if(numberAnswersSelected()){
    document.QuestionForm.QUESTIONNO.value--;
    document.QuestionForm.SUBMITTED.value = "TRUE";
    clearTimeout(TIMER);
    time = 0;
    SetCookie('username',time);
    document.QuestionForm.submit();
  }  
  return false;
}

function redirectToQuestion(questionNumber) {
  if(numberAnswersSelected()){
    document.QuestionForm.QUESTIONNO.value = questionNumber;
    document.QuestionForm.SUBMITTED.value = "TRUE";
    clearTimeout(TIMER);
    time = 0;
    SetCookie('username',time);
    document.QuestionForm.submit();
  }  
  return false;
}

function onReview() {
  if(numberAnswersSelected()){
    document.QuestionForm.REVIEW.value="TRUE";
    document.QuestionForm.SUBMITTED.value = "TRUE";
    clearTimeout(TIMER);
    time = 0;
    SetCookie('username',time);    
    document.QuestionForm.submit();
  }  
  return false;
}
function onReturnToTest(questionNumber) {
  if(document.forms['finishTestForm']) { 
    thisForm = document.finishTestForm;
  }
  if(document.forms['testInfoform']) { 
    thisForm = document.testInfoform;
  }
  thisForm.QUESTIONNO.value=questionNumber;
  clearTimeout(TIMER);
  time = 0;
  SetCookie('username',time);	
  thisForm.submit();
  return false;
}
function onNextQuestionSet(max,isForwards,offset) {
  if(isForwards==0) {
    max = max - offset;
  }
  document.statusform.BEGINAT.value=max;
  time = 0;
  SetCookie('username',time);	
  document.statusform.submit();
  return false;
}

function onRefresh() {
  if(navigator.appName != "Netscape" && document.QuestionForm.SUBMITTED.value != "TRUE")
  {
    document.QuestionForm.FINISH.value = "TRUE";
    document.QuestionForm.submit();
  }
}    

function showWarningMessage(num) {
  if(document.getElementById) {
    document.getElementById("lay1").style.visibility = (num==1)?"visible":"hidden";
    document.getElementById("lay2").style.visibility = (num==2)?"visible":"hidden";
    document.getElementById("lay3").style.visibility = (num==3)?"visible":"hidden";
  }
  else if (document.all){
    document.all("lay1").style.visibility = (num==1)?"visible":"hidden";
    document.all("lay2").style.visibility = (num==2)?"visible":"hidden";
    document.all("lay3").style.visibility = (num==3)?"visible":"hidden";
  }
}
function removeWarningMessage() {
   document.QuestionForm.WARNINGDISPLAYED.value="FALSE";
   for(i = 0;i<document.images.length;i++) {
     if(document.images[i].name == "Message") {
       document.images[i].src = "img/timeRemainingBlank.gif"
     }
   }
}

function onQuitSample() {
    document.QuestionForm.submit();
    window.close();
}

function initForm() {
  if(window.name=='test') {
    if(document.all) {
      try {
        window.moveTo(0,0);
      }
      catch(e) {
      }  
    }
    else {
      window.moveTo(0,0);
    }
    if(document.all) {
      try {
        if (window.resizeTo) {
          window.resizeTo(screen.availWidth,screen.availHeight);
        }
      }
      catch(e) {
      }  
    }
    else {
        if (window.resizeTo) {
          window.resizeTo(screen.availWidth,screen.availHeight);
        }
    }
    if (window.outerHeight) {
      window.outerHeight = screen.availHeight;
      window.outerWidth = screen.availWidth; 
    } 
    setLoaded();
  }
}
function highlightRows() {
  // overwritten in RenderQuestion.jspf
}
function rolloverRow(thisRow) {
      if(document.getElementById('option'+thisRow).checked) {
        document.getElementById('option'+thisRow+'row').className='highlightRow'
      }
      else {
        document.getElementById('option'+thisRow+'row').className='rolloverRow'
      }
}
function selectRow(thisRow) {
//alert(thisRow)
    if(document.getElementById('option'+thisRow)) {
      if(!(document.getElementById('option'+thisRow).type=='radio'&&document.getElementById('option'+thisRow).checked)) {
        document.getElementById('option'+thisRow).checked = !document.getElementById('option'+thisRow).checked;
      }  
    }
    highlightRows();
 }

function setLoaded()
{
  highlightRows();
  if(window.opener&&window.opener.top) {
    window.opener.top.setTestWindowFocus();
  }
}

function play_sound(source)
{
  try {
    document.all['soundcell'].src=source;
    return false;
  }
  catch(e) {
  }  
}
function keyNav(e) {
//alert(e.keyCode)
 if(e.keyCode == 49)
   {
     selectRow(1)
   }
 else if(e.keyCode == 50)
   {
     selectRow(2)
   }
 else if(e.keyCode == 51)
   {
     selectRow(3)
   }
 else if(e.keyCode == 52)
   {
     selectRow(4)
   }
 

 else if(e.keyCode == 56||e.keyCode == 112)
   {
   onPreviousQuestion()
   }
 
 else if(e.keyCode == 57||e.keyCode == 110)
   {
   onNextQuestion()
   }
 else if(e.keyCode == 102)
   {
  onFinishTest()
   }
 
 else if(e.keyCode == 104)
   {
  onPlaySound()
   }
}  
function onFinishTest() {
  document.QuestionForm.FINISH.value = "TRUE";
  document.QuestionForm.SUBMITTED.value = "TRUE";
  clearTimeout(TIMER);
  time = 0;
  SetCookie('username',time);    
//  document.QuestionForm.submit();
    window.close();
  return false;
}

function redirectToQuestion(questionNumber) {
  document.QuestionForm.QUESTIONNO.value = questionNumber;
  document.QuestionForm.SUBMITTED.value = "TRUE";
  clearTimeout(TIMER);
  time = 0;
  SetCookie('username',time);
//  document.QuestionForm.submit();
  questionNumber=document.QuestionForm.QUESTIONNO.value
  location.replace('Example'+questionNumber+'.htm');
  return false;
}
function onNextQuestion(questionNumber) {
  document.QuestionForm.QUESTIONNO.value++;
  document.QuestionForm.SUBMITTED.value = "TRUE";
  clearTimeout(TIMER);
  time = 0;
  SetCookie('username',time);
//  document.QuestionForm.submit();
  questionNumber=document.QuestionForm.QUESTIONNO.value
  location.replace('Example'+questionNumber+'.htm');

  return false;
}

function onPreviousQuestion(questionNumber) {
  document.QuestionForm.QUESTIONNO.value--;
  document.QuestionForm.SUBMITTED.value = "TRUE";
  clearTimeout(TIMER);
  time = 0;
  SetCookie('username',time);
//  document.QuestionForm.submit();
  questionNumber=document.QuestionForm.QUESTIONNO.value
  location.replace('Example'+questionNumber+'.htm');

  return false;
}

