// the following functions are common to all question types //
function format_time()
{ start = document.timer.start.value;
  finish = new Date();
  finish = finish.getTime();
  /* Seconds */
  s_elapsed = (finish - start) / 60000;
  s_round   = Math.round(s_elapsed);
  s_remain  = s_elapsed - s_round;
  s_remain  = (s_remain < 0) ? s_remain = 60 - ((s_round - s_elapsed) * 60) : s_remain = (s_elapsed - s_round) * 60;
  s_remain  = Math.round(s_remain);
  if (s_remain < 10)
     s_remain="0"+s_remain;
  /* Minutes */
  m_elapsed = (finish - start) / (60 * 60 * 1000);
  m_round   = Math.round(m_elapsed);
  m_remain  = m_elapsed - m_round;
  m_remain  = (m_remain < 0) ? m_remain = 60 - ((m_round - m_elapsed) * 60) : m_remain = ((m_elapsed - m_round) * 60);
  m_remain  = Math.round(m_remain - 0.495);
  if (m_remain < 10)
     m_remain="0"+m_remain;
  /* Hours */
  h_elapsed = (finish - start) / (24 * 60 * 60 * 1000);
  h_round   = Math.round(h_elapsed);
  h_remain  = h_elapsed - h_round;
  h_remain  = (h_remain < 0) ? h_remain = 24 - ((h_round - h_elapsed) * 24)  : h_remain = ((h_elapsed - h_round) * 24);
  h_remain  = Math.round(h_remain - 0.5);
  if (h_remain < 10)
     h_remain="0"+h_remain;
  document.timer.finish.value=h_remain+":"+m_remain+":"+s_remain;}
  
function errormessage (message)
{ mywindow=window.open('','win_error','toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=260,height=220');
  ndoc=mywindow.document;
  ndoc.close();
  ndoc.open();
  astr ='<html><head><title>Advice</title>.<style>h1 { font-size: 12pt;color: #000000;text-align: center; font-family: Verdana, Arial, Helvetica, sans-serif; margin: 5px 5px 5px 5px; padding: 5px 5px 5px 5px; border: 1px #000000 solid;  background: #FFFFF0;}';
  astr+='.exchange{color: #000000;border: 1px #000000 solid; padding: 2pt 2pt 2pt 2pt; margin: 2% 2% 2% 2%; font-family :Verdana, Helvetica, Arial,  sans-serif}';
  astr+='.feedback {font-size: 10pt; padding: 1pt 1pt 1pt 1pt; margin: 1% 1% 1% 1%; background-color: #F0F0F0; border: 1px #000000 solid}';
  astr+='.button { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; background-color: #FAF0D7; border: 1px solid Black; font-weight: bold;}</style></head><body>';
  astr+='<table class="exchange"><tbody><th><h1>Advice</h1></th><tr><td class="feedback">'+message+'</td></tr></tr>';
  astr+='<tr><td><form name="myform"><center><input class="button" type="button" name="closing" value="Close this Window" onclick= "window.close()"></center></form></td></tr></tbody></table>';
  astr+='</body></html>';
  ndoc.write (astr);
  mywindow.focus();}
  
function check_givefeedback(q)
{ if (eval('document.question_'+q+'.work.value')!="true")
     errormessage ('<em>Try to answer the Question!</em><br/>Feedback will be available once your responses are marked!');
  else
     givefeedback(q);}

function givefeedback(q)
{ mywindow=window.open('','win_feedback','toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=400,height=500');
  ndoc=mywindow.document;
  ndoc.close();
  ndoc.open();
  astr ='<html><head><title>Feedback</title>.<style>h1 { font-size: 12pt;color: #000000;text-align: center; font-family: Verdana, Arial, Helvetica, sans-serif; margin: 5px 5px 5px 5px; padding: 5px 5px 5px 5px; border: 1px #000000 solid;  background: #FFFFF0;}';
  astr+='h3 {font-size: 10pt;text-transform: none;color: Navy;  text-align: left; border-bottom-width : thin; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 5pt}';
  astr+='.exchange{color: #000000;border: 1px #000000 solid; padding: 2pt 2pt 2pt 2pt; margin: 2% 2% 2% 2%; font-family :Verdana, Helvetica, Arial,  sans-serif}';
  astr+='.feedback {font-size: 10pt; padding: 1pt 1pt 1pt 1pt; margin: 1% 1% 1% 1%; background-color: #F0F0F0; border: 1px #000000 solid}';
  astr+='.button { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; background-color: #FAF0D7; border: 1px solid Black; font-weight: bold;}</style></head><body>';
  astr+='<table class="exchange"><tbody>';
  astr+='<th><h1>Feedback on Question&nbsp;'+q+'</h1></th>';
  astr+='<tr><td class="feedback">';
  astr+=eval('document.question_'+q+'.feedback.value');
  astr+='</td></tr>';
  astr+='<th nowrap="nowrap"><h3>Link for Further Information</h3></th>';
  astr+='<tr nowrap="nowrap"><td>';
  if (eval('document.question_'+q+'.link.value') != "")
     astr+='<a href="'+eval('document.question_'+q+'.link.value')+'" target="newWindow">Click here</a>';
  else
     astr+='No link available at this stage';
  astr+='</td></tr><tr nowrap="nowrap"><form>';
  astr+='<td align="center"><input class="button" type="button" name="closing" value="   Close   "  onclick="window.close();"></td></form></tr></tbody></table>';
  astr+='</body></html>';
  ndoc.write(astr);
  mywindow.focus();}

function scoring (i, ans)
{ form=eval('document.question_'+i);
  if (form.attempt.value==0 && ans==1)
     form.score.value=form.vscore1.value;
  else if (form.attempt.value==1 && ans==1 && form.score.value==0)
     form.score.value=form.vscore2.value;
  form.f_score.value=form.score.value;
  if (form.found.value==0 && ans==1)
     { form.found.value=1;
       ++form.attempt.value;}
  else if (form.found.value==0)
     ++form.attempt.value;
  form.work.value=true;}

function checkScore(form)
{ form.f_score.value=form.score.value;}

function stripInput (key)
{ while(key.substring(0,1) == "-"  || key.substring(0,1) == " ")
     key=key.substring(1,key.length);
  while(key.substring(key.length-1,key.length) == "-" || key.substring(key.length-1,key.length) == " ")
     key = key.substring(0,key.length-1);
  return key;}

function randomizer (gapIndex, keyword)
{ newKeyword=new Array(gapIndex);
  if (gapIndex<=1)
     return keyword;
  for ( i=0 ; i<gapIndex ; i++ )
  {  indy=0;
     while (indy==0)
     {  j=Math.round(Math.random()*10);
        if (j>=0 && j<gapIndex && keyword[j]!="")
        {  newKeyword[i]=keyword[j];
           keyword[j]="";
           indy=1;}}}
  return newKeyword;}

function check_ans(q)
{ format_time();
  yourscore=0; maxscore=0;
  mywindow=window.open('','win_ans','toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=620,height=600');
  ndoc=mywindow.document;
  ndoc.close();
  ndoc.open();
  evaluation='<html><head><title>Results</title></head><body>';
  evaluation+='<table><tbody>';
  evaluation+='<th colspan="7"><h1>RESULTS OF YOUR TEST.</h1></th>';
  evaluation+='<tr>';
  evaluation+='<td><h4>Question.</h4></td>';
  evaluation+='<td><h4>Correct Selection(s)</h4></td>';
  evaluation+='<td><h4>Incorrect Selection(s)</h4></td>';
  evaluation+='<td><h4>Your Mark</h4></td>';
  evaluation+='<td><h4>Maximum Mark</h4></td>';
  evaluation+='<td><h4>Attempt</h4></td>';
  evaluation+='<td><h4>Feedback</h4></td></tr>';
  for (i=1; i<=q ; i++)
  {  if (eval('document.question_'+i+'.type.value')=="fillq")
        fill_check(i);
     else if (eval('document.question_'+i+'.type.value')=="imcq")
        imccheck(i);
     else if (eval('document.question_'+i+'.type.value')=="imrq")
        iboxcheck(i);
     else if (eval('document.question_'+i+'.type.value')=="mcq")
        mccheck(i);
     else if (eval('document.question_'+i+'.type.value')=="mrq")
        boxcheck(i);
     else if (eval('document.question_'+i+'.type.value')=="olq")
        orderedList(i);
     else if (eval('document.question_'+i+'.type.value')=="order")
        order_check(i);
     else if (eval('document.question_'+i+'.type.value')=="wmq")
        shortcheck(i);
     else
        alert("unknow type for question "+i);}
  pct=(100*yourscore)/maxscore;
  pct=pct.toString();
  string_location = pct.indexOf(".",0);
  if (string_location != -1)
     pct = pct.substring(0,string_location+3);
  evaluation+='<tr>';
  evaluation+='<td colspan="4"><b>You have '+yourscore+' out of '+maxscore+' possible marks.</b></td>';
  evaluation+='<td colspan="3" align="center"><b>'+pct+' %'+'</b></td></tr>';
  evaluation+='<tr><form>';
  evaluation+='<td colspan="4">It took you&nbsp;&nbsp;&nbsp;'+document.timer.finish.value+'&nbsp;&nbsp;&nbsp;to complete this test</td>';
  evaluation+='<td colspan="3" align="center"><input class="button" type="button" name="close" value="   Close   "  onclick="window.close();"></td></tr>';
  evaluation+='<tr><td colspan="7" align="center"><a href="http://www.centralbudapest.com/"><img alt="central budapest banner" src="http://www.centralbudapest.com/pictures/template/header.gif" border="0" name="central budapest logo"/></a></td></tr></table>';
  evaluation+='</body></html>';
  ndoc.write (evaluation);
  mywindow.focus();}

function show_ans(q)
{ if (document.question_1.work.value!="true")
  {  errormessage ('Please try answering the questions first!');
     return;}
  for (i=1; i<=q ; i++)
  {  if (eval('document.question_'+i+'.type.value')=="fillq")
        fill_reveal(i);
     else if (eval('document.question_'+i+'.type.value')=="imcq")
        ilmccheck(i);
     else if (eval('document.question_'+i+'.type.value')=="imrq")
        ilboxcheck(i);
     else if (eval('document.question_'+i+'.type.value')=="mcq")
        lmccheck(i);
     else if (eval('document.question_'+i+'.type.value')=="mrq")
        lboxcheck(i);
     else if (eval('document.question_'+i+'.type.value')=="olq")
        showOrderedList(i);
     else if (eval('document.question_'+i+'.type.value')=="order")
        order_reveal(i);
     else if (eval('document.question_'+i+'.type.value')=="wmq")
        lshortcheck(i);
     else
        alert("unknow type for question "+i);}}
// end of common functions //