<?php
   Template::Show('login_temp_head.php');
   ?>
<form id="crewtest" action="<?php echo url('/registration/EndCrewTest'); ?>" method="post" enctype="multipart/form-data" class="form-horizontal form-bordered">
   <div style="overflow: auto; height: 400px; margin-bottom: 20px; padding: 5px; padding-top: 0px; padding-bottom: 20px;">


    	<p class="text-center">
    		<br>
    		Examination for : <strong><?php echo $_POST['examinee_email']; ?></strong>
    		<br>

    		<h3 class="text-center" id="clockdiv"></h3>
    	</p>
      <?php

         $exam_id = 5;
         $questions = ExamsData::get_exam($exam_id);
         $howmany_questions = 10;
         $title = ExamsData::get_exam_title($exam_id);

         ?>
      <table class="table table-responsive">
         <?php
            $count = 0;
            foreach ($questions as $question) {
                $count++;
                echo '<tr><td align="left" width="15%">#'.$count.'</td>';
                echo '<td align="left"><b>'.$question->question.'</b><br />';
                echo '<input type="radio" name="question'.$count.'" value="1"> '.$question->answer_1.'<br>';
                echo '<input type="radio" name="question'.$count.'" value="2"> '.$question->answer_2.'<br>';
                echo '<input type="radio" name="question'.$count.'" value="3"> '.$question->answer_3.'<br>';
                echo '<input type="radio" name="question'.$count.'" value="4"> '.$question->answer_4.'<br>';
                echo '<input type="hidden" name="question_id'.$count.'" value="'.$question->id.'" />';
                echo '<input type="hidden" name="question_counter" value="'.$count.'" />';
                echo '</td></tr>';
            	if($count == 10) {
            		break;
            	}
            }
            ?>
         <tr>
            <td colspan="2">
               <input type="hidden" name="exam_id" value="<?php echo $title->id; ?>" />
               <input type="hidden" name="version" value="<?php echo $title->version; ?>" />
               <input type="hidden" name="passing" value="<?php echo $title->passing; ?>" />
               <input type="hidden" name="exam_description" value="<?php echo $title->exam_description; ?>" />
               <input type="hidden" name="howmany" value="<?php echo $howmany_questions; ?>" />
               <input type="hidden" name="examinee_email" value="<?php echo $_POST['examinee_email']; ?>" />


               <!--<input type="submit" value="Submit Exam">-->
            </td>
         </tr>
      </table>
   </div>
   <div class="form-group form-actions">
      <div class="col-md-12">
         <button type="submit" class="btn btn-sm btn-success btn-block">Submit Exam</button>
      </div>
   </div>
</form>
<!-- END Login Form -->
</div>
<!-- END Login Block -->
</div>
</div>
</div>
</div>
<!-- Login Container -->
<!-- END Login Container -->

		<script>
         // This code will autosubmit the form in 10 minutes // that is, 300000 milliseconds
         	var t = setTimeout("document.getElementById('crewtest').submit();", 600000);
    	</script>
    	<script>
         // 10 minutes from now
         var time_in_minutes = 10;
         var current_time = Date.parse(new Date());
         var deadline = new Date(current_time + time_in_minutes*60*1000);


         function time_remaining(endtime){
         var t = Date.parse(endtime) - Date.parse(new Date());
         var seconds = Math.floor( (t/1000) % 60 );
         var minutes = Math.floor( (t/1000/60) % 60 );
         var hours = Math.floor( (t/(1000*60*60)) % 24 );
         var days = Math.floor( t/(1000*60*60*24) );
         return {'total':t, 'days':days, 'hours':hours, 'minutes':minutes, 'seconds':seconds};
         }
         function run_clock(id,endtime){
         var clock = document.getElementById(id);
         function update_clock(){
         	var t = time_remaining(endtime);
         	clock.innerHTML = 'Time remaining : <strong>'+t.minutes+':'+t.seconds+'</strong>';
         	if(t.total<=0){ clearInterval(timeinterval); }
         }
         update_clock(); // run function once at first to avoid delay
         var timeinterval = setInterval(update_clock,1000);
         }
         run_clock('clockdiv',deadline);

         </script>
         <script>
         	setInterval(checkFocus, 2);
			function checkFocus() {
				var info = document.getElementById("message");
					if ( document.hasFocus() ) {
					// Do nothing!
					} else {
					alert("Cheating is not tolerated, You were already warned not to switch tabs while giving exams. You have been auto-failed by our system! You can request another exam after 12 hours!");
					var t = setTimeout("document.getElementById('crewtest').submit();", 4000);
					}
				}

         </script>
