var results = new Array(46);

// verbal: 0
// numerical1: 12
// numerical2: 17
// work rate: 26
// spatial1: 30
// spatial2: 32
// electrical: 34
// mechanical: 38
// memory1: 42
// memory2: 44

var correct = new Array("E","B","C","B","B","D","C","D","E","C","A","E",
"B","A","C","A","D",
"C","A","E","D","C","D","C","B","A",
"B","D","A","D",
"D","D",
"A","C",
"D","C","B","C",
"C","A","B","E",
"C","B",
"D","B");

var score= new Array(0,0,0,0,0,0,0,0);
var testTimer = 0;
var seconds = 0;
var div2_="invisiblediv";
var div3_="invisiblediv";

/********************************************************************************
Copyright (C) 1999 Thomas Brattli
This script is made by and copyrighted to Thomas Brattli at www.bratta.com
Visit for more great scripts. This may be used freely as long as this msg is intact!
I will also appriciate any links you could give me.
********************************************************************************/
//Default browsercheck, added to all scripts!
function checkBrowser(){
	this.ver=navigator.appVersion
	this.dom=document.getElementById?1:0
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
	this.ie4=(document.all && !this.dom)?1:0;
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5)
	return this
}
bw=new checkBrowser()
//Shows the div
function show(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	obj.display='block'
}
//Hides the div
function hide(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	obj.display='none'
}
//span block
//function blockSpan(div,nest){
//	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
//        obj.display='block'
//}
//span none
//function noneSpan(div,nest){
//	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
//        obj.display='none'
//}
//span visible
function showSpan(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	obj.visibility='visible'
}
//span invisible
function hideSpan(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	obj.visibility='hidden'
}

/********************************************/

function uncheckRadios() {
	var formsCollection = document.getElementsByTagName("form");
	for(var i=0;i<formsCollection.length;i++) {
	   //alert(formsCollection[i].name);
           frm = formsCollection[i].name;
	   for( j = 0; j < document[frm].radio_q.length; j++ ) {
	   	document[frm].radio_q[j].checked = false;
	   }
	}
}

function disableAllRadios() {
	var formsCollection = document.getElementsByTagName("form");
	for(var i=0;i<formsCollection.length;i++) {
	   //alert(formsCollection[i].name);
           frm = formsCollection[i].name;
	   for( j = 0; j < document[frm].radio_q.length; j++ ) {
	   	document[frm].radio_q[j].disabled = true;
	   }
	}
}

function enableAllRadios() {
	var formsCollection = document.getElementsByTagName("form");
	for(var i=0;i<formsCollection.length;i++) {
	   //alert(formsCollection[i].name);
           frm = formsCollection[i].name;
	   for( j = 0; j < document[frm].radio_q.length; j++ ) {
	   	document[frm].radio_q[j].disabled = false;
	   }
	}
}

function hideTicks() {
	hideSpan('test1q1_1tick');
	hideSpan('test1q1_2tick');
	hideSpan('test1q1_3tick');
	hideSpan('test1q2_1tick');
	hideSpan('test1q2_2tick');
	hideSpan('test1q2_3tick');
	hideSpan('test1q3_1tick');
	hideSpan('test1q3_2tick');
	hideSpan('test1q4_1tick');
	hideSpan('test1q4_2tick');
	hideSpan('test1q4_3tick');
	hideSpan('test1q4_4tick');
	hideSpan('test2q1_1tick');
	hideSpan('test2q1_2tick');
	hideSpan('test2q1_3tick');
	hideSpan('test2q1_4tick');
	hideSpan('test2q1_5tick');
	hideSpan('test2q2_1tick');
	hideSpan('test2q2_2tick');
	hideSpan('test2q2_3tick');
	hideSpan('test2q2_4tick');
	hideSpan('test2q2_5tick');
	hideSpan('test2q2_6tick');
	hideSpan('test2q2_7tick');
	hideSpan('test2q2_8tick');
	hideSpan('test2q2_9tick');
	hideSpan('test3q1_1tick');
	hideSpan('test3q1_2tick');
	hideSpan('test3q1_3tick');
	hideSpan('test3q1_4tick');
	hideSpan('test4q1_1tick');
	hideSpan('test4q1_2tick');
	hideSpan('test4q2_1tick');
	hideSpan('test4q2_2tick');
	hideSpan('test5q1_1tick');
	hideSpan('test5q1_2tick');
	hideSpan('test5q1_3tick');
	hideSpan('test5q1_4tick');
	hideSpan('test6q1_1tick');
	hideSpan('test6q1_2tick');
	hideSpan('test6q1_3tick');
	hideSpan('test6q2_1tick');
	hideSpan('test7q1_1tick');
	hideSpan('test7q1_2tick');
	hideSpan('test7q2_1tick');
	hideSpan('test7q2_2tick');
}

function hideRevealButtons() {
	hideSpan('reveal1q1_1tick');
	hideSpan('reveal1q1_2tick');
	hideSpan('reveal1q1_3tick');
	hideSpan('reveal1q2_1tick');
	hideSpan('reveal1q2_2tick');
	hideSpan('reveal1q2_3tick');
	hideSpan('reveal1q3_1tick');
	hideSpan('reveal1q3_2tick');
	hideSpan('reveal1q4_1tick');
	hideSpan('reveal1q4_2tick');
	hideSpan('reveal1q4_3tick');
	hideSpan('reveal1q4_4tick');
	hideSpan('reveal2q1_1tick');
	hideSpan('reveal2q1_2tick');
	hideSpan('reveal2q1_3tick');
	hideSpan('reveal2q1_4tick');
	hideSpan('reveal2q1_5tick');
	hideSpan('reveal2q2_1tick');
	hideSpan('reveal2q2_2tick');
	hideSpan('reveal2q2_3tick');
	hideSpan('reveal2q2_4tick');
	hideSpan('reveal2q2_5tick');
	hideSpan('reveal2q2_6tick');
	hideSpan('reveal2q2_7tick');
	hideSpan('reveal2q2_8tick');
	hideSpan('reveal2q2_9tick');
	hideSpan('reveal3q1_1tick');
	hideSpan('reveal3q1_2tick');
	hideSpan('reveal3q1_3tick');
	hideSpan('reveal3q1_4tick');
	hideSpan('reveal4q1_1tick');
	hideSpan('reveal4q1_2tick');
	hideSpan('reveal4q2_1tick');
	hideSpan('reveal4q2_2tick');
	hideSpan('reveal5q1_1tick');
	hideSpan('reveal5q1_2tick');
	hideSpan('reveal5q1_3tick');
	hideSpan('reveal5q1_4tick');
	hideSpan('reveal6q1_1tick');
	hideSpan('reveal6q1_2tick');
	hideSpan('reveal6q1_3tick');
	hideSpan('reveal6q2_1tick');
	hideSpan('reveal7q1_1tick');
	hideSpan('reveal7q1_2tick');
	hideSpan('reveal7q2_1tick');
	hideSpan('reveal7q2_2tick');
}

function resetForms() {
        uncheckRadios();
        hideTicks();
        hideRevealButtons();
        enableAllRadios();
        resetDivs();
}

function resetDivs() {
	//hide('intro');
		show('test1q1_1');
		hide('test1q1_3');
	hide('test1q1');
		show('test1q2_1');
		hide('test1q2_3');
	hide('test1q2');
		show('test1q3_1');
		hide('test1q3_2');
	hide('test1q3');
		show('test1q4_1');
		hide('test1q4_4');
	hide('test1q4');
		show('test2q1_1');
		hide('test2q1_5');
	hide('test2q1');
		show('test2q2_1');
		hide('test2q2_9');
	hide('test2q2');
	hide('test3_intro1');
	hide('test3_intro2');
		show('test3q1_1');
		hide('test3q1_4');
	hide('test3q1');
	hide('test4_intro1');
	hide('test4_intro2');
		show('test4q1_1');
		hide('test4q1_2');
	hide('test4q1');
	hide('test4q2_intro1');
	hide('test4q2_intro2');
	//hide('test4q2_intro3');
		show('test4q2_1');
		hide('test4q2_2');
	hide('test4q2');
		show('test5q1_1');
		hide('test5q1_4');
	hide('test5q1');
		show('test6q1_1');
		hide('test6q1_3');
	hide('test6q1');
		show('test6q2_1');
	hide('test6q2');

//	hide('test7q1_intro1');
//		hide('test7q1_1c');
//	hide('test7q1_1');
//              show('test7q1_1a');
//		hide('test7q1_2c');
//	hide('test7q1_2');
//	hide('test7q2_intro1');
//	hide('test7q2_intro2');
//		hide('test7q2_1c');
//	hide('test7q2_1');
//              show('test7q2_1a');
//		hide('test7q2_2c');
//	hide('test7q2_2');
//	hide('endtest');

	hide('test7q1_1');
                show('test7q1_1a');
		hide('test7q1_1c');
	hide('test7q1_2');
                show('test7q1_2a');
		hide('test7q1_2c');
	hide('test7q2_1');
                show('test7q2_1a');
		hide('test7q2_1c');
	hide('test7q2_2');
                show('test7q2_2a');
		hide('test7q2_2c');

	hide('preptips');
	
	show('intro');
}

//******** JQuery ready ********
$(document).ready(function(){
   //alert("JQuery ready!!");
   myBodyOnload();
});

function myBodyOnload() {

	//writeCSS('http://www.rafcareers.com/');
        resetDivs()

        //introClick(); //!!!!!!!!!!!!!!
}

function getSelectedOption(frm) {
	for( i = 0; i < document[frm].radio_q.length; i++ ) {
	 if( document[frm].radio_q[i].checked == true ) {
	  return( document[frm].radio_q[i].value );
       	 }
	}
        return( "nothing" );
}

function introClick() {
	resetForms();
	hide('intro');
       	show('test1q1');
	//show('test1q3');
}

function revealTick(div_) {
        showSpan(div_);
        disableAllRadios();
}

function nextQuestionClick(frm,hide_div,show_div,questionIndex) {
        var selected = getSelectedOption(frm)
	if (selected=="nothing") {
                alert( "You must select an option before proceeding" );
                return;
	}
        results[questionIndex] = selected;

	//alert(results);

	enableAllRadios();

	hide(hide_div);
	show(show_div);
        hide(div3_); // needed for memory test 7
}

function lastQuestionClick(frm,hide_div,show_div,questionIndex) {

	var selected = getSelectedOption(frm)
	if (selected=="nothing") {
                alert( "You must select an option before proceeding" );
                return;
	}
        results[questionIndex] = selected;

        markTest();

	hide(hide_div);
	show(show_div);
        hide(div3_); // needed for memory test 7
}

function nextClick(hide_div,show_div) {
	hide(hide_div);
	show(show_div);
}

function startMemoryTest(div1, div2, div3) {
        //alert("wibble")
	
	div2_ = div2;
        div3_ = div3;

	//seconds = 60;
	//testTimer = setTimeout("doTimer()", 1000);

	hide(div1);
        show(div2);
        show("counterdiv");

	$('#q7counter').countdown({until:'+60s', format:'S',  onExpiry:doTimerExpire});
}

function doTimerExpire() {
        //alert("wibble!");
        $('#q7counter').countdown('destroy');
	hide(div2_);
        show(div3_);
        hide("counterdiv");
}


function doTimer() {
        seconds = seconds-1;
	var newText, content;
        if (seconds<1) {
        	clearTimeout(testTimer);

		//document.getElementById('q7counter').innerHTML= "01:00";
                $("span#q7counter").html("01:00");

		hide(div2_);
        	show(div3_);
        	hide("counterdiv");
        	return;
        }else{
		if (seconds>59) {
                 highDigits = "01";
		}else{
                 highDigits = "00";
		}

                if (seconds<10) {
                 leadingzero = "0";
		}else{
                 leadingzero = "";
		}

 		//document.getElementById('q7counter').innerHTML= highDigits + ":" + leadingzero + seconds;
                $("span#q7counter").html(highDigits + ":" + leadingzero + seconds);

/*
		//create text node.
  		newText = document.createTextNode( highDigits + ":" + leadingzero + seconds );
  		//select the content element.
  		content = document.getElementById('q7counter');
  		//Ensure that the content element has been found.
		if(content !== null){
                 removeAllChildNodes(content);
		 //if (content.hasChildNodes()) {
		 // content.removeChild(content.LastChild);
		 //}
		 content.appendChild(newText);
                }
*/

		testTimer = setTimeout("doTimer()", 1000);
        }
}

function disableRadios(frm, n) {

	document[frm].radio_q[0].disabled="true";
        //disableRadios('t1q1_1', 5)
}

function removeAllChildNodes(cell) {
	if ( cell.hasChildNodes() ) {
	    while ( cell.childNodes.length >= 1 ) {
	        cell.removeChild( cell.firstChild );
	    }
	}
}

function skipTimer() {
        seconds = 0;
}

function endtestClick() {
        self.close();
        //alert("end test");

	//hide('preptips');
        //show('endtest');
}

function viewPreptips() {
        //alert("wibble");

	hide('endtest');
        show('preptips');
}

function returnFromPreptips() {
        //alert("wibble")

	hide('preptips');
        show('endtest');
}

function retakeTest() {
        //alert("retake test")

	resetForms();
	hide('endtest');
        show('intro');
}

function markTest() {
	var line1;
        var line2;

	calcScore(1,0,12);
        //alert("score0="+score0);
        calcScore(2,12,26);
        calcScore(3,26,30);
        calcScore(4,30,34);
        calcScore(5,34,38);
        calcScore(6,38,42);
        calcScore(7,42,46);

	score[0] = score[1] + score[2] + score[3] + score[4] + score[5] + score[6] + score[7];

	document.getElementById('result1').innerHTML= score[1];
        document.getElementById('result2').innerHTML= score[2];
        document.getElementById('result3').innerHTML= score[3];
        document.getElementById('result4').innerHTML= score[4];
        document.getElementById('result5').innerHTML= score[5];
        document.getElementById('result6').innerHTML= score[6];
        document.getElementById('result7').innerHTML= score[7];

	document.getElementById('scorecard').innerHTML = score[0] + " out of 46";

	showSpan('retaketest');
        if (score[0]==46) {
	        line1="Perfect result.";
	        line2=" ";
	        hideSpan('retaketest');
        }else if (score[0]>40){
		line1="Excellent result.";
		line2=" ";
        }else if (score[0]>30){
	        line1="Well done. Why not try the test again and make sure";
	        line2="you think carefully before you answer.";
        }else if (score[0]>20){
	        line1="Good effort. Try the test again, make sure you read";
	        line2="the questions thoroughly and think carefully before you answer.";
        }else if (score[0]>10){
	        line1="Only fair. Try the test again, make sure you read";
	        line2="the questions thoroughly and think carefully before you answer.";
        }else{
	        line1="Hard luck. Try the test again, make sure you read";
	        line2="the questions thoroughly and think carefully before you answer.";
	}
	document.getElementById('commentLine1').innerHTML= line1;
        document.getElementById('commentLine2').innerHTML= line2;
}

function calcScore(indx,start,end) {
	score[indx]=0;
	for(i=start; i<end; i++) {
                if (results[i] == correct[i]) {
                        score[indx]+=1;
		}
	}
}

