﻿// JScript File***
var global_currentTest;
var global_currentMessager;
var global_mcatInfo;



function hideElement(element_id) {
    var obj;
    if (typeof element_id == 'string') {
        obj = document.getElementById(element_id)
    } else {
        obj = element_id;
    }
    if (obj && obj.style) {
        obj.style.visibility = "hidden";
        obj.style.display = "none";
    }
}
function showElement(element_id) {
    var obj;
    if (typeof element_id == 'string') {
        obj = document.getElementById(element_id)
    } else {
        obj = element_id;
    }
    if (obj && obj.style) {
        obj.style.visibility = "visible";
        obj.style.display = ""
    }
    
}

function highLight(obj){
        if(global_currentTest.selectionLocked == true){
            return;
        }
		while (obj.tagName!="TD"){
		    //alert("obj.className" + obj.className)
			obj=obj.parentNode
		}
		//alert("obj.className = " + obj.className)
		switch(obj.className){       
		    case "answerSpan":
		        obj.className = "answerSpan_highlight"
		        break;
		    case "answerSpan_highlight":
		        break;
		    case "answerSpanSelected":
		        obj.className = "answerSpanSelected_highlight"
		        break;
		    case "answerSpanSelected_highlight":
		        break;
	        case "answerSpan_green_highlight":
	            break;
	        case "answerSpanSelected_green_highlight":
	            break;
	        case "answerSpanDark":
	            obj.className = "answerSpanDark_highlight"
	            break;
	        }
		//obj.className += "_highlight";

}
function unhighLight(obj){
        if(global_currentTest.selectionLocked == true){
            return;
        }
        while (obj.tagName!="TD"){
			        obj=obj.parentNode
		        }	
		switch(obj.className){       
		    case "answerSpan":
		        break;
		    case "answerSpan_highlight":
		        obj.className = "answerSpan";
		        break;
		    case "answerSpanSelected":
		        break;
		    case "answerSpanSelected_highlight":
		        obj.className = "answerSpanSelected";
		        break;
	        case "answerSpan_green_highlight":
	            break;
	        case "answerSpanSelected_green_highlight":
	            break;
	        case "answerSpanDark_highlight":
	            obj.className = "answerSpanDark";
	            break;
	        }
		    
		        //obj.className=obj.className.substring(0,obj.className.length-10)
		        //alert("class = " +  obj.className)
		        
    
}
function selectAnswer(obj){
        if(global_currentTest.selectionLocked == true){
            return;
        }
        var i=0;
        var answerNumber=0;
        var tmpObj;
       answers_td = document.getElementById("answerContainer");
       
       //deselect all the others, and select the 'obj'
       for(i=1; i <= answers_td.getElementsByTagName("TABLE").length ;i++){
           
            tmpObj = document.getElementById('answer'+i)
            if (tmpObj){
                if(tmpObj == obj){
                    tmpObj.className = "answerSpanSelected";
                    global_currentTest.answersArray[global_currentTest.currentGlobalIndex]=i;
                }
                else{
                tmpObj.className = "answerSpan" ;
                }
               }
          }
      
       //mark as 'answered' in the navBar.
      global_currentTest.navObj.answeredArray[global_currentTest.currentGlobalIndex] = true;
}
function selectMultiAnswer(obj){//used with AMI type
        if(global_currentTest.selectionLocked == true){
            return;
        }    
    if(obj.className.split("_")[0] == "answerSpanSelected"){
       obj.className = "answerSpan"
       }
    else{
        obj.className ="answerSpanSelected";
        }
    var answers = document.getElementsByName("correctImg")   
    var selectedArray = new Array(answers.length);
    
    for(var i=0; i < answers.length; i++){
        if(answers[i].parentNode.parentNode.parentNode.firstChild.className == "answerSpanSelected")
                selectedArray[i] = true;
            else
                selectedArray[i] = false; 
    }
    global_currentTest.answersArray[global_currentTest.currentGlobalIndex]= selectedArray;
    global_currentTest.navObj.answeredArray[global_currentTest.currentGlobalIndex] = true;
}
function selectFromGroup(obj,max){
        if(global_currentTest.selectionLocked == true){
            return;
        }    if(obj.className.split("_")[0] == "answerSpanSelected"){
        obj.className ="answerSpan";
    }
    else{
        var columns = obj.parentNode.childNodes;
        var countSelected = 0;
        //count how many are already selected.
        for(var i=1; i < columns.length;i++){
            if(columns[i].className.split("_")[0] == "answerSpanSelected")
                countSelected++;
        }
        if(countSelected >= max){
            alert("Select only " + max + " options from each row.");
            return;
            }
        else{
          obj.className ="answerSpanSelected";  
        }
    }//end of outer most 'else'
    var selectionArray = new Array()
    var rows = obj.parentNode.parentNode.childNodes;
    
    for(i=0; i < rows.length; i++){
    
        selectionArray[i] = new Array();//add a row to the array
        
        for(var j=0; j < rows[i].childNodes.length; j++){
            //add the elements in the row.
            
            if(rows[i].childNodes[j].className.split("_")[0] == "answerSpanSelected"){
                 selectionArray[i][j] = true;
                 }
            else{
                selectionArray[i][j] = false;
                }
            
        } 
            
    }//end of outer for
   
//    var str;
//    for(i=0 ;i < selectionArray.length; i++ ){
//        str += "\n";
//        for(j=0; j < selectionArray[i].length ; j++){
//          str += selectionArray[i][j].toString();  
//        }
//    }
    //alert(selectionArray[1][1].toString());
    
    global_currentTest.answersArray[global_currentTest.currentGlobalIndex]= selectionArray;
    global_currentTest.navObj.answeredArray[global_currentTest.currentGlobalIndex] = true;
    
}
function selectStatment(obj){
    
    if(global_currentTest.selectionLocked == true){
        return;
    }
    if(obj.className == "answerSpanSelected")
        return;
    
    obj.className = "answerSpanSelected"
    
    var mostArray = document.getElementsByName("most");
    var leastArray = document.getElementsByName("least");
    
    if(obj.firstChild.name == "most"){
        for(var i=0; i < mostArray.length; i++){
            if(mostArray[i].parentNode == obj)
                leastArray[i].parentNode.className = "answerSpan"
            else
               mostArray[i].parentNode.className =  "answerSpan"
        }
    }
        
    if(obj.firstChild.name == "least"){
        for(i=0; i < leastArray.length; i++){
            if(leastArray[i].parentNode == obj)
               mostArray[i].parentNode.className = "answerSpan"
            else
                leastArray[i].parentNode.className =  "answerSpan"
        } 
    }
    var users_selection = new Array();
    users_selection[0] = null;
    users_selection[1] = null;
    for(i=0; i < leastArray.length; i++){
            if(leastArray[i].parentNode.className == "answerSpanSelected")
                users_selection[1] = i;
            if(mostArray[i].parentNode.className == "answerSpanSelected")
                users_selection[0] = i;
    }
    
    
    global_currentTest.answersArray[global_currentTest.currentGlobalIndex]= users_selection;
    global_currentTest.navObj.answeredArray[global_currentTest.currentGlobalIndex] = (users_selection[0] != null && users_selection[1] != null) ;
}
/******************************************************************************************************/
 /*****************************************  MCAT ******************************************************/
 /******************************************************************************************************/
 function MCAT(testGUID) { 
    this.testGUID = testGUID;
    this.testPlayer = global_currentTest;
    this.strikes = new Object();
    this.htmlText = new Object();
    this.personalNotes = new Object();

//    this._mcatFrameIsReady = false;
//    this._


    this.toggleAnswerSelection =
    function toggleAnswerSelection(questionNum, answerNum, answerCount) {
        var answerDiv = this.getAnswerDiv(questionNum, answerNum);
        switch (answerDiv.className) {
            case "answerOpt answerOpt_gray":
                //Deselect all the others:
                for (var i = 1; i <= answerCount; i++) {
                    var answerNode = document.getElementById("answerDiv_" + questionNum + "_" + i)
                    showElement(this.getStrikeSpan(questionNum, i));
                    answerNode.className = "answerOpt answerOpt_gray";
                }
                answerDiv.className = "answerOpt answerOpt_selected";
                //hide the Strike button:
                hideElement(this.getStrikeSpan(questionNum, answerNum));
                this.toggleStrike(questionNum, answerNum, false, answerCount);
                this.mcatSaveAnswerInClient(questionNum, answerNum);
                break;
            case "answerOpt answerOpt_selected":
                answerDiv.className = "answerOpt answerOpt_gray";
                showElement(this.getStrikeSpan(questionNum, answerNum));
                break;
            default:
                alert("wtf? : " + childDiv.innerHTML)
        }
        if (!this.strikes[global_currentTest.currentQuestionID]) {
            this.strikes[global_currentTest.currentQuestionID]
        }
    };

    this.answerIsSelected =
    function answerIsSelected(questionNum, answerNum) {
        return this.getAnswerDiv(questionNum, answerNum).className == "answerOpt answerOpt_selected"
    };
    
    this.answerIsStriked =
    function answerIsStriked(questionNum, answerNum) {
        return this.getAnswerTR(questionNum, answerNum).className == "striked";
    };

    this.mcatSaveAnswerInClient =
    function mcatSaveAnswerInClient(questionNum, answerNum) {
        var answerObj = global_currentTest.answersArray[global_currentTest.currentGlobalIndex];
        if (!answerObj) {
            var input = document.getElementById("mcatQuestionCountInput");
            if (!input)
                throw Error("(MCAT)mcatSaveAnswerInClient: Could not find questionCountInput");
            answerObj = new Array(Number(input.value));
            for (var i = 0; i < answerObj.length; i++) {
                answerObj[i] = null;
            }
            global_currentTest.answersArray[global_currentTest.currentGlobalIndex] = answerObj;
        }
        answerObj[questionNum - 1] = answerNum - 1;
        global_currentTest.answersArray[global_currentTest.currentGlobalIndex] = answerObj;
        global_currentTest.navObj.answeredArray[global_currentTest.currentGlobalIndex] = true;
    };


    this.toggleStrike =
    function toggleStrike(questionNum, answerNum, needToStrikeOverride, answersCount) {
        //if (answersCount == null) alert("MCAT.toggleStrike() : answersCount is null");
        var answerTR = this.getAnswerTR(questionNum, answerNum);
        var strikeSpan = this.getStrikeSpan(questionNum, answerNum);
        var needToStrike = (needToStrikeOverride != null) ? needToStrikeOverride : (answerTR.className != "striked");
        answerTR.className = (needToStrike) ? "striked" : "";
        if (needToStrike) {
            hideElement("strike_" + questionNum + "_" + answerNum);
            showElement("unstrike_" + questionNum + "_" + answerNum);
        } else {
            showElement("strike_" + questionNum + "_" + answerNum);
            hideElement("unstrike_" + questionNum + "_" + answerNum);
        }

        //If striked all except one, automatically select the remaining one .
        if (answersCount != null) {
            var unstrikedAnswers = new Array();
            for (var i = 0; i < answersCount; i++) {
                if (this.getAnswerTR(questionNum, Number(i + 1)).className != "striked") {
                    unstrikedAnswers.push(i + 1);
                }
            }
            if (unstrikedAnswers.length == 1) {
                if (!this.answerIsSelected(questionNum, unstrikedAnswers[0])) {
                    this.toggleAnswerSelection(questionNum, unstrikedAnswers[0], answersCount);
                }
            }
        }
        

    };

    this.getStrikeSpan =
    function getStrikeSpan(questionNum, answerNum) {
        return document.getElementById("strikeSpan_" + questionNum + "_" + answerNum);

    };

    this.getAnswerDiv =
    function getAnswerDiv(questionNum, answerNum) {
        return document.getElementById("answerDiv_" + questionNum + "_" + answerNum);
    };

    this.getAnswerTR =
    function getAnswerTR(questionNum, answerNum) {
        return document.getElementById("answerTR_" + questionNum + "_" + answerNum);
    };

    this.toggleMcatExplanation =
    function toggleMcatExplanation(questionNum, hide_bool) {
        //If specific question not specified, toggle all.
        if (questionNum == null) {
            var qCount = this.subQuestionsCount();
            for (var i = 0; i < qCount; i++) {
                this.toggleMcatExplanation(i + 1, hide_bool);
            }
            return;
        }

        var func = (hide_bool) ? hideElement : showElement;
        var answersObj = global_currentTest.answersArray[global_currentTest.currentGlobalIndex];

        //Show/Hide 'X' beside the user's answers.
  
        if (answersObj != null && answersObj[questionNum - 1] != null) {
    
            func("wrongImg_" + questionNum + "_" + (Number(answersObj[questionNum - 1]) + 1).toString());

        }
        //Show/Hide the "v" image
        func("correctImg_" + questionNum);

        //Show/Hide Explnation:
        func("ExpDiv_" + questionNum)

        //Show/Hide buttons:
        if (hide_bool) {
            showElement("showExpBtn_" + questionNum)
        } else {
            hideElement("showExpBtn_" + questionNum)
        }


    };

    this.submitExpFeedBack =
    function submitExpFeedBack(formName) {
        var formObj = document.forms[formName]
        var rateValue;
        for (var i = 0; i < formObj.radioRate.length; i++) {
            if (formObj.radioRate[i].checked) {
                rateValue = formObj.radioRate[i].value;
                break;
            }
            rateValue = "0";
        }
        var textContent = formObj.freeText.value;
        submitExpFeedBackGeneric(rateValue, textContent, formName);
    };
    
    this.subQuestionsCount = function subQuestionsCount(){
        return document.getElementById("mcat_questionCount").value
    }
    this.saveMcatInfo =
    function saveMcatInfo() {
        var questionID = global_currentTest.currentQuestionID();
        var subQCount = this.subQuestionsCount();

        //HTML Text:
        //if (this.htmlText[questionID] == null) {
        this.htmlText[questionID] = document.getElementById('mcat_frame').contentWindow.getHtmlText()
        //}

        //Strikes:
        //if (this.strikes[questionID] == null) {
        this.strikes[questionID] = new Array();
        var currentStrikeObj = this.strikes[questionID];
        //for each sub-question
        for (var i = 0; i < subQCount; i++) {
            currentStrikeObj.push(new Array());
            //get Strikes (for each optional answer):
            for (var j = 0; j < 4; j++) {

                //break out on questions with less that 4 sub answers
                if (this.getAnswerTR(i + 1, j + 1) == null)
                    break;
                (currentStrikeObj[i]).push(this.answerIsStriked(i + 1, j + 1))
            }
        }
        //}
        var strikedString = global_currentTest.encodeAnswer(this.strikes[questionID])

        //Personal Notes:
        if (global_currentTest.mode == "step_by_step") {
            this.personalNotes[questionID] = new Array();
            var currentNotesObj = this.personalNotes[questionID];
            //for each sub-question
            for (var i = 0; i < subQCount; i++) {
                currentNotesObj.push(document.getElementById("personalNote_" + (i + 1)).value);
            }
        }
        var personalNotesString = global_currentTest.encodeAnswer(this.personalNotes[questionID]);

        var testGUID = document.getElementById("test_root").getAttribute("testGUID");
        var url = "http://" + document.location.host + "/jobtestprep/getQuestion.asmx";
        var pl = new SOAPClientParameters();
        pl.add("testGUID", testGUID);
        pl.add("question_id", questionID);
        pl.add("htmlText", this.htmlText[questionID]);
        pl.add("strikes", strikedString);
        pl.add("personalNotes", personalNotesString);
        SOAPClient.invoke(url, "saveMcatInfo", pl, true, this.saveMcatInfo_callBack);
    }
    this.saveMcatInfo_callBack = 
        function saveMcatInfo_callBack(response) {
            //alert("mcat_saveMcatInfo_callBack : " + response);
        }
    this.loadMcatInfo = function loadMcatInfo() {
            var questionID = global_currentTest.currentQuestionID();

            //html text:
            if (this.htmlText[questionID] != null) {
                if (document.getElementById('mcat_frame').contentWindow.setHtmlText == null)
                    document.getElementById("mcat_source").innerHTML = this.htmlText[questionID];
                else
                    document.getElementById('mcat_frame').contentWindow.setHtmlText(this.htmlText[questionID]);
            } else {
                if (document.getElementById('mcat_frame').contentWindow.setHtmlText != null) { 
                    var textToSet = document.getElementById("mcat_source").innerHTML;
                    //alert("setting: \n" + textToSet)
                    document.getElementById('mcat_frame').contentWindow.setHtmlText(textToSet);
                }
                
            }

    

            //strikes:
            if (this.strikes[questionID] != null) {
                var qCount = this.subQuestionsCount();
                for (var i = 0; i < qCount; i++) {
                    for (var j = 0; j < 4; j++) {
                        if (this.getAnswerTR(i + 1, j + 1) == null)
                            break;
                        if (this.strikes[questionID][i][j])
                            this.toggleStrike(i + 1, j + 1);
                    }
                }
            }

            //personal notes:
            if (this.personalNotes[questionID] != null && this.personalNotes[questionID] != "null") {
                for (var i = 0; i < this.personalNotes[questionID].length; i++) {
                    var value = this.personalNotes[questionID][i];
                    selectValue("personalNote_" + (i + 1), value);
                }
            }

        }
    this.fetchMcatInfo =
        function fetchMcatInfo() {
            var testGUID = document.getElementById("test_root").getAttribute("testGUID");
            var questionID = global_currentTest.currentQuestionID();
            var url = "http://" + document.location.host + "/jobtestprep/getQuestion.asmx";
            var pl = new SOAPClientParameters();
            pl.add("testGUID", testGUID);
            pl.add("question_id", questionID);
            SOAPClient.invoke(url, "getMcatInfo", pl, true, this.fetchMcatInfo_callBack);
        }
        this.fetchMcatInfo_callBack =
        function fetchMcatInfo_callBack(obj) {
            if (obj.htmlText != null)
                global_currentTest.MCAT.htmlText[obj.questionID] = obj.htmlText;
            if (obj.strikes != null)
                global_currentTest.MCAT.strikes[obj.questionID] = decodeAnswer(obj.strikes);
            if (obj.personalNotes != null)
                global_currentTest.MCAT.personalNotes[obj.questionID] = decodeAnswer(obj.personalNotes);
            global_currentTest.MCAT.loadMcatInfo();
        }

        this.needToFetch = function needToFetch(questionID) {
            return (this.htmlText[questionID] == null || this.strikes[questionID] == null || this.personalNotes[questionID] == null);
        }

    } //End of MCAT Class
 
 /******************************************************************************************************/
 /***************************************** End of   MCAT **********************************************/
 /******************************************************************************************************/
    function storeText(obj) {
        try {
            global_currentTest.answersArray[global_currentTest.currentGlobalIndex] = obj.value;
        } catch (e) {
            global_currentTest.answersArray[global_currentTest.currentGlobalIndex] = obj.innerHTML;
        }
  global_currentTest.navObj.answeredArray[global_currentTest.currentGlobalIndex] = true;
    
}
function storePshycText(cutomerGUID,textObj){
    var text =textObj.innerHTML
    
    text = escape(text)
    text = text.replace(/%0D%0A/g,"<br/>");
    text = unescape(text)
    var url = "http://" + document.location.host + "/jobtestprep/getQuestion.asmx";
    var pl = new SOAPClientParameters();
    pl.add("customerGUID",cutomerGUID);
    pl.add("textContent",text);
    SOAPClient.invoke(url, "storePshycText", pl, true, storePshycText_callBack);
}
function storePshycText_callBack(response){
    switch (response){
        case "0":
            alert("Error saving FeedBack to DataBase");
        break;
        case "1":
            alert("FeedBack saved")
        break;
        default:
            alert("Unknown error saving to DataBase");
    }
    return;
}
function submitExpFeedBack(){
    var formObj = document.expFeedbackForm;
    var rateValue;
    for (var i=0; i < formObj.radioRate.length; i++){
        if (formObj.radioRate[i].checked){
            rateValue = formObj.radioRate[i].value;
            break;
        }
       rateValue = "0";
    }
    var textContent = formObj.freeText.innerHTML;
    submitExpFeedBackGeneric(rateValue, textContent);
}

function submitExpFeedBackGeneric(rateValue, textContent,formName) {
    var testGUID = document.getElementById("test_root").getAttribute("testGUID");
    var question_id = document.getElementById("test_root").childNodes[global_currentTest.currentSection].childNodes[global_currentTest.currentIndex].getAttribute("id");
    var url = "http://" + document.location.host + "/jobtestprep/getQuestion.asmx";
    var pl = new SOAPClientParameters();
    var sLanguage = document.getElementById("sLanguage").getAttribute("sLanguage");
    pl.add("testGUID", testGUID);
    pl.add("textContent", textContent);
    pl.add("rateValue", rateValue);
    pl.add("question_id", question_id + ((formName==null)?"":"\\" + formName.split("_")[1]));
    pl.add("formName", (formName == null) ? "expFeedbackForm" : formName)
    pl.add("sLanguage", sLanguage);
    SOAPClient.invoke(url, "storeExpFeedback", pl, true, storeExpFeedback_callBack);
}
function storeExpFeedback_callBack(response) {
    if (typeof (response) == "string")
        response = eval("(" + response + ")"); //"{msg:'Feedback received.',formName:'expFeedbackForm_1'}"
    
    var idSuffix = ""
    if (response.formName.split("_").length > 1) {
        idSuffix = "_" + response.formName.split("_")[1];
    }

    var radioTD = document.getElementById("radioButtonsTD" + idSuffix);
    var textAreaTD = document.getElementById("textAreaTD" + idSuffix);
    if(radioTD != null && textAreaTD != null){
        radioTD.innerHTML = "";
        textAreaTD.innerHTML = response.msg;
    }else{
        alert("NULL")
    }
    document.forms[response.formName].submitButton.disabled = true;
}
function toggleTickBox(obj){ // used with the BCN type.

    if(global_currentTest.selectionLocked == true){
        return;
    }
    if(obj.className.split('_')[0] == "answerSpan"){
        obj.className = "answerSpanSelected";
        }
    else{
       obj.className = "answerSpan" ;
       }
       
    var  trueBoxes = document.getElementsByName("tickBoxTrue");
    var falseBoxes = document.getElementsByName("tickBoxFalse");
    
    var users_selection = new Array();
    for(var i=0; i < trueBoxes.length;i++){
        //Deselect the other box if needed:
        if(obj == trueBoxes[i].parentNode && obj.className == "answerSpanSelected"){
           falseBoxes[i].parentNode.className = "answerSpan"; 
        }
        if(obj == falseBoxes[i].parentNode && obj.className == "answerSpanSelected"){
           trueBoxes[i].parentNode.className = "answerSpan"; 
        }
        //If 'identical' is selected:
        if(trueBoxes[i].parentNode.className == "answerSpanSelected"){
            users_selection[i] = new Array(false,true,false,false)
            }
        else if(falseBoxes[i].parentNode.className == "answerSpanSelected"){
            users_selection[i] = new Array(false,false,true,false)
            }
        else
          users_selection[i] = new Array(false,false,false,false)  
    }
    global_currentTest.answersArray[global_currentTest.currentGlobalIndex]= users_selection;
    global_currentTest.navObj.answeredArray[global_currentTest.currentGlobalIndex] = true;
    
}
function showSolvingTips(){
    if(global_currentMessager == null)
        return
    
   // global_currentTest.pauseTest();
    global_currentMessager.showMessage('solvingTips');
    return;
}
	
/****************************************************************************************/
/***************************** TEST PLAYER **********************************************/
/****************************************************************************************/

function testPlayer(name){
    this.lockedNavigation
    this.name = name
    this.testHasStarted ;             //used to indicate that the test has started, for timing.
    this.testSubmitted;              //Indicates if the test has been submitted.
    this.testXML                     //the XML containing the test data.
    this.questionCount               //how many questions in the test
    this.currentGlobalIndex          //the global index of the currently playing question.
    this.currentIndex                //the index ,relative to the current section.
    this.currentSection              //the current section in the test.
    this.sectionCount                //how many sections in the test
    this.testTimer                   //a timer object.
    this.explanationVisible = false  //indicates if the explanations are currently visible.
    this.navObj                      //current navigation object assigned to this test.
    this.answersArray = new Array();
    this.mode                        //the playing mode of the test (step_by_step,  regular timed mode or report mode.)
    this.engine                     // 1 = DAR , 0 = Aptitude , 2 = open questions, 3 = MCAT
    this.messager
    this.selectionLocked
    this.messagesArray = new Array()        //holds the section-messages
    this.lockedQuestionArray = new Array() //questions that the user cannot enter.(true = locked)
    this.MCAT;
    this.questionNum_to_be_confirmed; //used in goto function when changing sections.
    this.setTestXML = function setTestXML(){
                                        this.testXML = document.getElementById("test_root")
                                        }
                                
    this.init = function init(){
                            window.focus(); //Used to focus while opening new windows from report.
                            var i;
                            this.lockedNavigation = false;
                            this.selectionLocked = false;
                            this.setTestXML();
                            this.currentIndex = 0;
                            this.currentGlobalIndex = 0;
                            this.currentSection = 0;
                            
                            this.sectionCount = this.testXML.childNodes.length;
                            this.questionCount = countQuestions(this.testXML,this.answersArray,this.messagesArray);
                            if(this.messagesArray.length==0)this.messagesArray=null //if no section messages , set it to null
                            this.testHasStarted = false;
                            this.testSubmitted = false;
                            this.testTimer = new countdown(this.name + ".testTimer");
                            
                            switch (this.testXML.getAttribute("mode")) {
                               
                                    case "step_by_step" :
                                        this.mode = "step_by_step";
                                        hideElement("top_bar_divider");
                                        hideElement("top_bar_clock");
                                    break;
                                    case "timed":
                                        this.mode = "timed";
                                        if(this.testXML.getAttribute("analysis_engine") == "1" || this.testXML.getAttribute("analysis_engine")== "2"){
                                           hideElement("top_bar_divider");
                                           hideElement("top_bar_clock");
                                           
                                        }
                                        //Hide the tips button.
                                        hideElement("tipsBtn");
                                        document.getElementById("firstDevider").className = "top_bar_tile"
                                        break;
                                    case "report":
                                        this.mode = "report";
                                        hideElement("q_list");
                                        hideElement("top_bar");
                                        this.selectionLocked = true;
                                        break;
                                    default :
                                        this.mode = "step_by_step";
                                        //alert("Unknown player mode , using default");
                                 }
                                 var msg = new Messager("messageRoot","");
                                 //alert("calling startMessage (init)")
                                 document.getElementById("topBarTestName").innerHTML = msg.getText("startMessage","testName")
                                 this.engine = this.testXML.getAttribute("analysis_engine")
                                 this.setUrls();
                                 
                            
                            }
   this.startTest = function startTest(){
                        if(testP.questionCount != 0 ){
                            var paramStr = window.location.search.substring(1);
                            var paramArray = paramStr.split("&");
                            //alert(paramArray.toString())
                            var gotoQ=""
                            for(var i=0; i < paramArray.length;i++){
                                if(paramArray[i].split("=")[0] == "goto"){
                                    gotoQ = paramArray[i].split("=")[1]
                                    break;
                                }
                            }
                            if(gotoQ != ""){
                                testP.goTo(gotoQ);
                            }
                            else{
                            
                                if(this.messager != null && this.messagesArray != null){
                                   
                                    this.messager.showMessage(this.messagesArray[0]);
                                    
                                    }
                                testP.playCurrentQuestion();
                                }
                         }
                         else
                        {
                            alert("no questions")
                            hideElement("message_buttonText1")
                            document.getElementById("q_list").innerHTML = "&nbsp;Empty Test&nbsp;";
                        }
                    }                     
    
   this.playQuestion = function playQuestion(question_id, specific_type, sLanguage,users_answer){
   
                        //alert("q_id = " + question_id + "\nspecific_type = " + specific_type + "\nsLanguage = " + sLanguage + "\nusers_answer = " + users_answer)
                       if(this.lockedNavigation == true)
                            return;
                        this.lockedNavigation = true;
                        this.explanationVisible = false;
                        //alert("users_answer = " +  users_answer)
                        //document.location.href.replace("demo.htm", "getQuestion.asmx");
                        var url = "http://" + document.location.host + "/jobtestprep/getQuestion.asmx"
                       // alert("playQuestion : " + url + "\nquestion_id = " + question_id + "\n specific_type = " + specific_type + "\nsLanguage = " + sLanguage + "\nmode = " + this.mode)
                        var pl = new SOAPClientParameters();
		                pl.add("question_id",question_id);
                        pl.add("specific_type",specific_type);
                        pl.add("sLanguage",sLanguage);
                        pl.add("mode",this.mode);
                        //pl.add("answersArray",a)
                        //alert(question_id)
		                showElement("loading");
		                this.pauseTest();
		                SOAPClient.invoke(url, "getQuestion", pl, true, getQuestion_callBack);
		               
                        document.getElementById("question_count").innerHTML =  "<b>" + document.getElementById("questionTitle").getAttribute("value") + " : " + (this.currentGlobalIndex+1) + "/" + (this.questionCount)+"<b>";
                        /*if(users_answer != null && users_answer != "unanswered"){
                            document.getElementById("answerContainer").childNodes[parseInt(users_answer,10)].firstChild.firstChild.firstChild.className = "answerSpanSelected"
                           //alert( document.getElementById("answerContainer").childNodes[parseInt(users_answer,10)].tagName);
                           }*/
                        
		                }
  
   
   this.getLanguage = function getLanguage(){//get the pages' language
                        //alert("sLanguage = " + document.getElementById("sLanguage").getAttribute("sLanguage"))
                        return document.getElementById("sLanguage").getAttribute("sLanguage");
                    }


                    this.goTo = function goTo(questionNum) {//NOTE: the parameter is the Q number, NOT the index.

                        if (this.mode != "report") this.saveCurrentAnswer();
                        if (this.questionNum - 1 == 0) {
                            this.currentIndex = 0;

                            if (this.currentSection != 0 && this.messager != null && this.messagesArray != null) {
                                this.messager.showMessage(this.messagesArray[0]);
                                this.currentSection = 0;
                            }
                            this.navObj.markAsCurrent(questionNum);
                            this.currentGlobalIndex = questionNum - 1;
                            this.playCurrentQuestion();
                            return;
                        }




                        var i, j;
                        var count = 0;

                        for (i = 0; i < this.testXML.childNodes.length; i++) {
                            section = this.testXML.childNodes[i];
                            for (j = 0; j < section.childNodes.length; j++) {
                                if (section.childNodes[j].getAttribute("node_type") == "question")//just in case.
                                {
                                    count++;
                                    if (count == questionNum) {
                                        this.currentIndex = j;
                                        //if crossed a section:
                                        if (this.currentSection != i) {
                                            var section_time = section.getAttribute("section_time")
                                            if (!isNaN(parseInt(section_time)) && this.mode == "timed") {//timed sections cant be visited more than once.
                                                this.questionNum_to_be_confirmed = questionNum;
                                                this.messager.showMessage("timed_sections_confirm");
                                                return;
                                            }
                                            if (this.messager != null && this.messagesArray != null)
                                                this.messager.showMessage(this.messagesArray[i]);
                                        }
                                        this.currentSection = i;
                                        this.navObj.markAsCurrent(questionNum);
                                        this.currentGlobalIndex = questionNum - 1;
                                        this.playCurrentQuestion();
                                        return;
                                    }

                                }
                                else {
                                    alert("FUNC: playNextQuestion(q): Attempt to count a non-question node");
                                    return;
                                }
                            } //inner 'for'
                        } //outer 'for'


                    }           //end of func.

                    this.gotoConfirmed = function() {
                        var questionNum = this.questionNum_to_be_confirmed;
                        var count = 0;
                        for (var i = 0; i < this.testXML.childNodes.length; i++) {
                            section = this.testXML.childNodes[i];
                            for ( var j = 0; j < section.childNodes.length; j++) {
                                count++;
                                if (count == questionNum) {
                                    this.currentIndex = j;
                                    //if crossed a section:
                                    if (this.currentSection != i) {
                                        if (this.messager != null && this.messagesArray != null)
                                            this.messager.showMessage(this.messagesArray[i]);
                                        
                                        var section_time = section.getAttribute("section_time")
                                        if (!isNaN(parseInt(section_time))) {//in a MCAT test, you can't visit a section more than once.
                                            this.currentGlobalIndex = questionNum - 1;
                                            this.lockSection(this.currentSection);
                                            //Set the Timer to the new section-time:
                                            this.testTimer.Setup(section_time);
                                            this.currentSection = i;
                                            this.navObj.markAsCurrent(questionNum);
                                            
                                            this.playCurrentQuestion();
                                            return;
                                        }
                                    }
                                    
                                }
                            } //inner 'for'
                        } //outer 'for'
                    }   //end of gotoConfirmed               
     
     this.lockSection = function(sectionIndex) {
                        if (this.mode != "timed")
                            return;
                        var section = this.testXML.childNodes[sectionIndex];
                        var firstQInd = 0;
                        for (var i = 0; i < sectionIndex; i++) {
                            firstQInd += this.testXML.childNodes[i].childNodes.length;
                        }
                        for (var i = firstQInd; (i - firstQInd) < section.childNodes.length; i++) {
                            this.lockedQuestionArray[i] = true;
                        }
                        this.navObj.refresh();
                    }
    this.playNextQuestion = function playNextQuestion() {
                        this.goTo(this.currentGlobalIndex + 2); return;
                    }

                    this.playCurrentQuestion = function playCurrentQuestion() {

                        if (this.testHasStarted == false) {
                            this.testHasStarted = true;
                            if (this.testXML.getAttribute("time_limit") == "") {
                                this.testXML.setAttribute("time_limit", "356400"); //99 hours
                            }
                            
                            
                            var time_limit = this.testXML.childNodes[this.currentSection].getAttribute("section_time");
                            if (!isNaN(parseInt(time_limit))) {
                                this.testTimer.Setup(time_limit);//if time is specified on the section ==> use it
                            } else { 
                                this.testTimer.Setup(this.testXML.getAttribute("time_limit"));//otherwise use the global time limit
                            }
                            
                        }
                        //                                 alert(   "\n this.currentIndex = " +this.currentIndex+
                        //                                    "\n this.currentGlobalIndex = " + this.currentGlobalIndex+
                        //                                    "\n this.currentSection ="  + this.currentSection+
                        //                                    "\n this.sectionCount = " +this.sectionCount+
                        //                                    "\n this.questionCount = "+ this.questionCount)

                        var q = this.testXML.childNodes[this.currentSection].childNodes[this.currentIndex]
                        //alert("play current passing :" + this.answersArray[this.currentGlobalIndex])
                        //alert(q.getAttribute("id")+"\n"+q.getAttribute("Type")+"\n"+this.getLanguage())
                        this.playQuestion(q.getAttribute("id"), q.getAttribute("Type"), this.getLanguage(), this.answersArray[this.currentGlobalIndex]);

                    }
                    this.toggleExplanation = function toggleExplanation() {

                        var questionType = document.getElementById("type").innerHTML;

                        if (questionType == "ROZ") return;

                        if (questionType == "MCT" || questionType == "LST" || questionType == "GMT" || questionType == "GMV" || questionType == "MTQ" || questionType == "MC3") {
                            var questionNum = arguments[0];
                            var hide_bool = arguments[1];
                            if (!global_currentTest.MCAT)
                                global_currentTest.MCAT = new MCAT(global_currentTest.testGUID);
                            global_currentTest.MCAT.toggleMcatExplanation(questionNum, hide_bool);
                            // this.explanationVisible = !this.explanationVisible;
                            return;
                        }
                        if ((questionType == "AMI" || questionType == "CLR" || questionType == "CCH") && !this.explanationVisible) {
                            correct = document.getElementsByName("correctImg")
                            selected = this.answersArray[this.currentGlobalIndex];
                            var trueValue
                            //alert(selected[0].toString())
                            for (var i = 0; i < correct.length; i++) {
                                //alert("i = " + i + "\n selected.length = " + selected[0].length)
                                if (selected != null)
                                    switch (questionType) {
                                    case "AMI":
                                        trueValue = selected[i];
                                        break;
                                    case "CLR":
                                        trueValue = selected[parseInt(i / (selected[0].length - 2))][i % (selected[0].length - 2) + 1];
                                        break;
                                    case "CCH":
                                        trueValue = selected[parseInt(i / (selected[0].length - 1))][i % (selected[0].length - 1) + 1];
                                        break;
                                }

                                if (correct[i].getAttribute('correct') == "1" || (selected != null && trueValue)) {
                                    correct[i].style.visibility = "visible";
                                    correct[i].style.display = "";
                                }
                            } //end of for loop
                            this.explanationVisible = true;
                            return;
                        } //end of 'If'
                        //Show for BCN type:
                        if (questionType == "BCN" && !this.explanationVisible) {
                            correct = document.getElementsByName("correctImg")
                            selected = this.answersArray[this.currentGlobalIndex];
                            var currentAnswer = 0;
                            for (var i = 0; i < correct.length; i++) {
                                if (selected != null) {
                                    currentAnswer = 0
                                    if (selected[i][1]) currentAnswer = 1;
                                    if (selected[i][2]) currentAnswer = 2;
                                    var isIdentical = (correct[i].getAttribute('correct') == "1")
                                    if ((currentAnswer == 1 && isIdentical) || (currentAnswer == 2 && !isIdentical)) {
                                        //User Answered correctly, replace the default "X" image with a "V" image:
                                        correct[i].src = correct[i].src.replace("wrongAnswer.gif", "correct.jpg")
                                    } else {
                                        correct[i].src = correct[i].src.replace("correct.jpg", "wrongAnswer.gif")
                                    }
                                }
                                //Show the image:
                                correct[i].style.visibility = "visible";
                                correct[i].style.display = "";
                            } //end of FOR
                            this.explanationVisible = true;
                            return;
                        } //End of BCN type
                        if (document.getElementsByName("correctImg").length > 1 && this.explanationVisible) {

                            correct = document.getElementsByName("correctImg")
                            //alert("correct.length = " + correct.length )
                            for (var i = 0; i < correct.length; i++) {
                                correct[i].style.visibility = "hidden";
                                correct[i].style.display = "none";
                                this.explanationVisible = false;
                            }
                            return;
                        }
                        var correct = document.getElementById("correct");
                        if (this.explanationVisible) {
                            //remove highlight from the correct answer.
                            if (correct.parentNode.parentNode.nextSibling != null) {
                                correct = correct.parentNode.parentNode.nextSibling.firstChild;
                                if (correct.className != "answerSpanSelected") {
                                    correct.className = "answerSpan";

                                }
                                else {
                                    correct.className = "answerSpanSelected";
                                }
                            }
                            else {
                                correct = correct.parentNode.parentNode.parentNode.parentNode.firstChild;
                                if (correct.className != "answerSpanSelected") {
                                    correct.className = "answerSpan";
                                }
                                else {
                                    correct.className = "answerSpanSelected";
                                }
                            }

                            var gifArray = document.getElementsByName("wrongRightAnswer")
                            for (var i = 0; i < gifArray.length; i++) {
                                gifArray[i].style.visibility = "hidden"
                                gifArray[i].style.display = "none"
                            }
                            hideElement('explanation_text_table')
                            for (var i = 1; i < 12; i++) {

                                nextheight = (i) * (212 / 11)
                                nextx = -4 / (12 - i) - nextheight
                                moveAction = "document.getElementById('explanation').style.top='" + nextx + "px';document.getElementById('explanation').style.clip='rect(" + nextheight + "px 580px 226px 0px)'"
                                window.setTimeout(moveAction, i * 50)
                            }
                            window.setTimeout("hideElement('explanation');", 12 * 50)
                            window.setTimeout("hideElement('exBtnClose');", 12 * 50)
                            window.setTimeout("showElement('exBtn');", 12 * 50)
                            hideElement("correct")


                            this.explanationVisible = false;
                        } //end of outer (main) 'if'

                        else {//explanation NOT visible

                            //set the correct answer to be hightlighted.
                            if (correct.parentNode.parentNode.nextSibling != null) {
                                correct = correct.parentNode.parentNode.nextSibling.firstChild
                                if (correct.className != "answerSpanSelected") {
                                    correct.className = "answerSpan_green_highlight"; //green-highlight (border) only if not selected.
                                    if (this.answersArray[this.currentGlobalIndex] != null) {
                                        var gifArray = document.getElementsByName("wrongRightAnswer")//show the 'X' on the wrong answer.
                                        gifArray[this.answersArray[this.currentGlobalIndex] - 1].style.visibility = "visible";
                                        gifArray[this.answersArray[this.currentGlobalIndex] - 1].style.display = "";
                                    }
                                }


                            }

                            else {
                                correct = correct.parentNode.parentNode.parentNode.parentNode.firstChild
                                if (correct.className != "answerSpanSelected")
                                    correct.className = "answerSpan_green_highlight";
                                if (this.answersArray[this.currentGlobalIndex] != null) {
                                    var gifArray = document.getElementsByName("wrongRightAnswer")//show the 'X' on the wrong answer.
                                    gifArray[this.answersArray[this.currentGlobalIndex] - 1].style.visibility = "visible";
                                    gifArray[this.answersArray[this.currentGlobalIndex] - 1].style.display = "";
                                }
                            }

                            hideElement("explanation_text_table")

                            for (var i = 1; i < 12; i++) {
                                nextheight = (11 - i) * (212 / 11)
                                nextx = -4 / (12 - i) - nextheight
                                moveAction = "document.getElementById('explanation').style.top='" + nextx + "px';document.getElementById('explanation').style.clip='rect(" + nextheight + "px 580px 226px 0px)'"
                                window.setTimeout(moveAction, i * 50)
                            }
                            //	                            if(document.getElementById("explanation_text_table") != null)
                            //	                                alert(document.getElementById("explanation_text_table").innerHTML)

                            window.setTimeout("showElement('explanation_text_table')", 12 * 50)
                            window.setTimeout("showElement('exBtnClose');", 12 * 50)
                            window.setTimeout("hideElement('exBtn');", 12 * 50)
                            showElement("explanation");
                            showElement("correct");
                            this.explanationVisible = true;
                        }

                    }
  this.registerNavigator = function registerNavigator(navObj){//register a navigator listening for this test.
                                  
                                  this.navObj = navObj;
                            }
  this.registerMessager = function registerMessager(messagerObj){
                                  this.messager = messagerObj
                                  //alert("calling startMessage (register)")
                                  this.messager.showMessage("startMessage");
                                  //NOTE: the button in the startMessage should invoke
                                  //'testPlayer.startTest()' in order for the test to start playing.
                            }
                            this.saveCurrentAnswer = function saveCurrentAnswer() {

                                this.onSaveCurrentAnswer(); //type specific logic

                                 if (this.answersArray[this.currentGlobalIndex] == null) {
                                    //alert("saveCurrentAnswer: null answer, aborting.")
                                    return;
                                }
                                //alert(this.answersArray[this.currentGlobalIndex])


                                var GUID = document.getElementById("test_root").getAttribute("testGUID")
                                var url = "http://" + document.location.host + "/jobtestprep/getQuestion.asmx"
                                var pl = new SOAPClientParameters();
                                var question_id = document.getElementById("test_root").childNodes[this.currentSection].childNodes[this.currentIndex].getAttribute("id")
                                var answer = this.encodeAnswer(this.answersArray[this.currentGlobalIndex])

                                //alert("sending :\n" + answer + "\n length = " + answer.length)

                                var answering_time = stringToSec(document.getElementById("clock").innerHTML);
                                //if answering time could not be parsed , pass a value of '0'.
                                if (isNaN(parseInt(answering_time))) answering_time = "0";
                                var specific_type = document.getElementById("test_root").childNodes[this.currentSection].childNodes[this.currentIndex].getAttribute("Type")
                                //alert("GUID = " + GUID + "\nquestion_id = " + question_id + "\nanswer = " + answer + "\nanswer length = " + answer.length + "\nanswering_time = " + answering_time);

                                pl.add("testGUID", GUID);
                                pl.add("section_ind", this.currentSection);
                                pl.add("specific_type", specific_type);
                                pl.add("question_id", question_id);
                                pl.add("answer", answer)
                                pl.add("answering_time", answering_time)
                                SOAPClient.invoke(url, "saveAnswer", pl, false, saveAnswer_callBack);
                            }
  //fires just before the answer is sent
  this.onSaveCurrentAnswer = function() {
                                var type = document.getElementById("type")
                                if(type){
                                    switch (document.getElementById("type").innerHTML) {
                                        case "MTQ":
                                        case "GMT":
                                        case "LST":
                                        case "MCT":
                                        case "MC3":
                                            global_currentTest.MCAT.saveMcatInfo();
                                            break;
                                    }
                                }

                            }
     //fires at the end of the callback
                            this.onGetQuestion_callback = function () {
                                switch (document.getElementById("type").innerHTML) {
                                    case "MTQ":
                                    case "GMT":
                                        resizeMe();
                                    case "GMV":
                                    case "LST":
                                    case "MCT":
                                    case "MC3":
                                        //init the MCAT object if needed
                                        if (!this.MCAT)
                                            this.MCAT = new MCAT(this.testGUID);
                                        var questionID = global_currentTest.currentQuestionID();
                                        (this.MCAT.needToFetch(questionID)) ? this.MCAT.fetchMcatInfo() : this.MCAT.loadMcatInfo();
                                        var centerDiv = document.getElementById("centerdiv");
                                        if (centerDiv != null) {
                                            centerDiv.style.width = "99%";
                                        }

                                        break;
                                }
                            }
  this.encodeAnswer = function encodeAnswer(obj){
                        //if its an essay dont return the whole string.
                        //   if(document.getElementById("Type").innerText == "ESS")
                        //                return "ESSAY"
                        
                        if(obj == null)
                            return "null";
                        switch(typeof(obj)){
                            case "string":
                                return obj;
                                break;
                            case "boolean":
                                if(obj == true)
                                    return "t";
                                else
                                    return "f";
                            case "function":
                            case "number":
                                return obj.toString();
                                break;
                           case "object":
                           
                            var res = "[";
                            for(var i=0 ;i < obj.length; i++){
                                if(i > 0)
                                    res += ",";
                                res += this.encodeAnswer(obj[i]);
                            }
                             
                            return res + "]";
                           
                        }
                        
                      }
                      
//  this.decodeAnswer = function decodeAnswer(ans){
//                            var url = "http://" + document.location.host + "/jobtestprep/getQuestion.asmx";
//                            var pl = new SOAPClientParameters();
//                            SOAPClient.invoke(url, "getAnswers", pl, true, decodeAnswer_callBack);
//                      }
  this.submitTest = function submitTest(){
                            showElement("loading");
                             
                            var url = "http://" + document.location.host + "/jobtestprep/getQuestion.asmx";  
                            var testRoot = document.getElementById("test_root");
                            var testGUID = testRoot.getAttribute("testGUID");
                            var sLanguage = document.getElementById("sLanguage").getAttribute("sLanguage");
                            var engine;
                            if(testRoot.getAttribute("analysis_engine") != null){
                                    engine = testRoot.getAttribute("analysis_engine");
                                }
                                else{
                                    //alert("analysis engine not found , using value : ?");
                                    engine = "?"
                                }
                            var pl1 = new SOAPClientParameters();
                            
                            pl1.add("testGUID",testGUID) ;
                            //alert("from submitTest : analysis_engine = " +engine )
                            pl1.add("analysis_engine",engine);
                            pl1.add("sLanguage",sLanguage)
                            SOAPClient.invoke(url, "submitTest", pl1, false, submitTest_callBack);
                         
                    }
  function submitTest_callBack(r){
    if(r == null){
	   // alert("submitTest_callBack() : Warining: r == null")
		 hideElement("loading");
		return;
    }
//    if(r.indexOf("missingQuestions") != -1){
//		var startPos = r.indexOf(":") + 1;
//		var strArr = r.substr(startPos);
//		var missingQuestions = strArr.split(",");
//		global_currentTest.updateMissingQuestions(missingQuestions);
//		return;
//    }
    hideElement("loading");
    global_currentTest.testSubmitted = true;
    
    //alert("submitTest_callBack returned : " + r);
  }
  
  this.updateMissingQuestions = function (missingQuestions){
            alert("updateMissingQuestions")
			//The 'missingQuestions' contains the indexes of the questions missing on the server.
			var missingAnswers = new Array();
			
			for(var i=0; i < missingQuestions.length; i++){
				missingAnswers[i] = this.encodeAnswer(this.answersArray[missingQuestions[i]]);
			}	
			var GUID = document.getElementById("test_root").getAttribute("testGUID")
            var url = "http://" + document.location.host + "/jobtestprep/getQuestion.asmx"
            var pl = new SOAPClientParameters();
            var question_id = document.getElementById("test_root").childNodes[this.currentSection].childNodes[this.currentIndex].getAttribute("id")
            var answer = this.encodeAnswer(this.answersArray[this.currentGlobalIndex])

            pl.add("testGUID",GUID);
            pl.add("qIndexArray",missingQuestions);
            pl.add("answersArray",missingAnswers);
            SOAPClient.invoke(url, "updateMissingAnsweres", pl, false, updateMissingQuestions_callBack);					
			}
  function updateMissingQuestions_callBack(r){
	if(r == "OK"){
		global_currentTest.submitTest();
	}else{
		alert("updateMissingQuestions_callBack:\n" + r)
	}
  }
  this.timeElapsed = function timeElapsed() {
      if (this.mode != "timed" || this.engine == "1") {
          return
      }

      this.saveCurrentAnswer();

      //if only the SECTION time has elapsed
      if (!isNaN(parseInt(this.testXML.childNodes[this.currentSection].getAttribute("section_time")))) {
          this.lockSection(this.currentSection);
          //search and goTo the next unlocked question.
          for (var i = (this.currentGlobalIndex + 1) % this.questionCount;
                    i != this.currentGlobalIndex;
                    i = ++i % this.questionCount) {

              if (!this.lockedQuestionArray[i]) {
                  this.questionNum_to_be_confirmed = i + 1
                  return this.gotoConfirmed();
              }
          }
      }

      this.submitTest();
      if (this.messager != null)
          this.messager.showMessage('timeElapsed')
  }
  this.setUrls = function(){
                            setUrls();
                         }
  this.pauseTest = function(){
                        this.testTimer.CountActive = false;
                    }
  this.resumeTest = function(){
                         if(this.testTimer.CountActive == true){
                            return;
                         }else{
                            this.testTimer.CountActive = true;
                            var secs = stringToSec(document.getElementById("clock").innerHTML)
                            if(this.testHasStarted == true && !isNaN(parseInt(secs))){
								
                               this.testTimer.Setup(secs);
                            }
                            
                         }

                     }
    this.currentQuestionID = function() {
                         return document.getElementById("test_root").childNodes[this.currentSection].childNodes[this.currentIndex].getAttribute("id");
                     }
    this.init();
    
 }//end of class



 //1)counts how many questions in the test.
//2) decodes the answers (if resuming a previously started test) from the attributes on the SPAN elements.
function countQuestions(testXML, answersArray,messagesArray){
    //alert("countQuestions , test_info :"  + document.getElementById("test_root").innerHTML)
    var i,j;
    var count = 0;
    //if empty test
    if(!testXML.hasChildNodes() || testXML.firstChild.nodeType != 1){
      return; 
    }
    
    for(i=0; i<testXML.childNodes.length; i++){
        section = testXML.childNodes[i];
        if(section.getAttribute("intro_code")!= null && section.getAttribute("intro_code") != ""){
            messagesArray[i] = section.getAttribute("intro_code")
        }
       
        for(j=0; j<section.childNodes.length; j++){
            if(section.childNodes[j].getAttribute("node_type") == "question")//just in case.
            {
                count++;
                answersArray[count-1] = decodeAnswer(section.childNodes[j].getAttribute("answer"));
            }
            else
            {
                alert("FUNC: countQuestions : Attempt to count a non-question node");
                return count;
            }
            
        }//inner 'for'
    }//outer 'for'
    var debStr="";
    for(i=0;i < answersArray.length;i++){
        debStr += "\n " + answersArray[i];
    }
    //alert("after count: \n" + debStr);
    if(messagesArray.length == 0){
        messagesArray = null;
    }
    return count
}
function getQuestion_callBack(r) {
    document.getElementById("question_body").innerHTML = "?????????????????????????"
    document.getElementById("question_body").innerHTML = r.toString()

		if(global_currentTest == null){
		    alert("global_currentTest == null" + "\nr= \n" + r);
		}
		if(global_currentTest.currentGlobalIndex == global_currentTest.questionCount-1 && global_currentTest.mode != "report"){//if it is the last question.
            var NQ = document.getElementById("nextQuestion");
                NQ.onclick = function(){
                                            var tp = global_currentTest;
                                            tp.saveCurrentAnswer();
                                            //Check if DAR test had been fully answered:
                                            if(false){//if(tp.engine == "1"){
                                                var testComplete = true;
                                                var arr = tp.navObj.answeredArray
                                                if(arr.length != tp.questionCount){
                                                    testComplete = false;
                                                }else{
                                                    for(var i=0; i < arr.length; i++){
                                                        if( arr[i] == null || arr[i] == false ){
                                                          testComplete = false;
                                                          break;  
                                                        }
                                                    }//end of for loop
                                                }
                                                if(testComplete == false){
                                                   tp.messager.showMessage('notCompleteMessage');
                                                   return; 
                                                }
                                            }
                                            //If reached here then DAR test complete ,OR not a DAR test:
                                            tp.messager.showMessage('submitMessage');
                                        }
                if (NQ.nextSibling && NQ.nextSibling.onclick)
                    NQ.nextSibling.onclick = NQ.onclick
                if (NQ.previousSibling && NQ.previousSibling.onclick)
                     NQ.previousSibling.onclick = NQ.onclick
            
             NQ.innerHTML = document.getElementById("submitText").value
         }
        //hide the 'next question' and the 'explanation' buttons.
        
        //if in 'report' mode , then show the explanation.
		if(global_currentTest.mode == "report"){
		    if(global_currentTest.engine != '2'){
		        global_currentTest.toggleExplanation(); 
		        global_currentTest.explanationVisible = false;
		    }
		}
		//alert("toggled explanation")

        //hides both the 'explanation' button and the 'next question' button.
        if(document.getElementById("test_root").getAttribute("mode")=="report"){
            //if vcr type , allow to toggle the explanation
            var type = document.getElementById("type").innerHTML;
            var typesThatShow = new Array("VCR","DTH","NCR","FLT","DIR");
            for(var j=0; j < typesThatShow.length ;j++){
                if (type == typesThatShow[j]){
                    var button = document.getElementById("nextQuestion");
                    button.previousSibling.style.visibility = "hidden";
                    button.previousSibling.style.display = "none";
                    button.nextSibling.style.visibility = "hidden";
                    button.nextSibling.style.display = "none";
                    button.style.visibility = "hidden";
                    button.style.display = "none";
                    global_currentTest.explanationVisible = true;
                    break;
                }
            }
           if(j == typesThatShow.length){
                 document.getElementById("nextQuestion").parentNode.parentNode.style.visibility = "hidden";
                 document.getElementById("nextQuestion").parentNode.parentNode.style.display = "none";
            }
        }
        //If in 'report' mode , notify the user if the question was not answered.
        if(global_currentTest.mode == 'report'){
             var notification = document.getElementById("reportNotification");
            if(global_currentTest.answersArray[global_currentTest.currentGlobalIndex] == null){
                if(notification == null){return;/*alert("notification not found")*/}
                showElement("reportNotification");
            }else{
               hideElement("reportNotification");
            }
            
            //notification.innerHTML = "Unanswered Quesetion"
          }  
		var qType
		hideElement("loading");
		var users_answer = global_currentTest.answersArray[global_currentTest.currentGlobalIndex];
		if(document.getElementById("type") != null && users_answer != null){
		    qType = document.getElementById("type").innerHTML;
		    switch (qType){
    		    case "OPQ":
    		         //alert(" Answer Array = " + global_currentTest.answersArray)
    		        var mostArray = document.getElementsByName("most");
    		        
                    var leastArray = document.getElementsByName("least");
                   
                    if(users_answer[0] != null){
                         mostArray[users_answer[0]].parentNode.className = "answerSpanSelected"
                         }
                    if(users_answer[1] != null)
                         leastArray[users_answer[1]].parentNode.className = "answerSpanSelected"
    		    break;
    		    case "ROZ":
    		    case "TAT":
    		    case "ESS":
    		    case "HAS":
    		            var ans = global_currentTest.answersArray[global_currentTest.currentGlobalIndex];
    		            //alert("ans = " + ans);
    		            if(ans != null && ans == "<Linked>"){//if a text is present in the server.
    		                var url = "http://" + document.location.host + "/jobtestprep/getQuestion.asmx"
                            var pl = new SOAPClientParameters();
                            pl.add("testGUID",document.getElementById("test_root").getAttribute("testGUID"));
                            //alert("this.currentSection = " + global_currentTest.currentSection + "\n this.currentIndex = " + global_currentTest.currentIndex);
                            
                            pl.add("question_id",document.getElementById("test_root").childNodes[global_currentTest.currentSection].childNodes[global_currentTest.currentIndex].getAttribute("id"))
                            SOAPClient.invoke(url, "getLongAnswer", pl, true, getLongAnswer_callBack);  
                        }
                        if(ans != null)//if text is already cached in client -> diplay it.
                            document.getElementById("textArea").innerHTML = ans;  
    		    break;
    		    case "CCH":
    		    case "CLR":
    		    
                    var rows = document.getElementById("elementsTable").childNodes[1].childNodes;
                    
                    for(var i=0; i < rows.length; i++){
                        for(var j=0; j < rows[i].childNodes.length; j++){
                            if(users_answer[i][j] == true)
                                rows[i].childNodes[j].className = "answerSpanSelected";
                        }
                       
                    }
    		    break;
    		    case "AMI":
    		        var answers = document.getElementsByName("correctImg")
                    for(var i=0; i < answers.length; i++){
                    if(users_answer[i] == true)
                        answers[i].parentNode.parentNode.parentNode.firstChild.className = "answerSpanSelected"
                    }//end of for loop
    		    break;
    		    case "BCN":
    		        var trueAnswers = document.getElementsByName("tickBoxTrue");
    		        var falseAnswers = document.getElementsByName("tickBoxFalse");
    		        for(var i=0; i < trueAnswers.length; i++ ){
    		            if(users_answer[i][1] == true){
    		                trueAnswers[i].parentNode.className = "answerSpanSelected";
    		                }
    		            if(users_answer[i][2] == true){
    		                falseAnswers[i].parentNode.className = "answerSpanSelected";
    		                }
    		        }
    		        break;
                case "MTQ":
                case "GMT":
                case "GMV":
    		    case "LST":
    		    case "MCT":
    		        if (users_answer) {
    		            if (!global_currentTest.MCAT)
    		                global_currentTest.MCAT = new MCAT(global_currentTest.testGUID);
    		            for (var i = 0; i < users_answer.length; i++) {
    		                global_currentTest.MCAT.toggleAnswerSelection(i + 1, Number(users_answer[i]) + 1, 4)
    		            }
    		        }
    		        break;
    		    case "MC3":
    		        if (users_answer) {
    		            if (!global_currentTest.MCAT)
    		                global_currentTest.MCAT = new MCAT(global_currentTest.testGUID);
    		            for (var i = 0; i < users_answer.length; i++) {
    		                global_currentTest.MCAT.toggleAnswerSelection(i + 1, Number(users_answer[i]) + 1, 3)
    		            }
    		        }
    		        break;
    		    default://for simple multiple choice questions.
    		         global_currentTest.selectionLocked = false;
    		         selectAnswer(document.getElementById('answer'+ users_answer));
    		         if(document.getElementById("checkedQuestions") != null)
    		                global_currentTest.selectionLocked = true;
    		    break;
    		
		    }//end of switch
		}//end of if

		global_currentTest.onGetQuestion_callback();

		hideElement('exBtnClose');
	    showElement('exBtn');
        global_currentTest.resumeTest();
		global_currentTest.lockedNavigation = false;
	}
function saveAnswer_callBack(r){
    if(r != "OK"){
        alert("saveAnswer_callback = " + r.toString());
    }
}
function decodeAnswer(t){
    var i ,j;
    var elArray;
    if(t=="")//if empty string
        return null;
    if(typeof(t) == "string" && t.charAt(0) == '['){//if object
       
       if(t.charAt(1) == '['){//2-D array
            elArray = t.substring(2, t.length-2).split("],[")
            for(i=0; i < elArray.length; i++){
                elArray[i] =  elArray[i].split(",");
                for(j=0; j < elArray[i].length;j++){
                    if(elArray[i][j]=='t'){
                        elArray[i][j]=true;
                    }else{
                        elArray[i][j]=false;
                        } 
                } 
            }
        }
        else{//1-D array
             elArray = t.substring(1,t.length-1).split(",");
             for(i=0; i < elArray.length; i++){
                                    
                if(elArray[i]=='t'){
                    elArray[i]=true;
                    continue;
                }
                if (elArray[i]=='t'){
                    elArray[i]=false;
                    continue;
                }
                if(elArray[i] == "null"){
                    elArray[i] = null;
                }
                
             }  
        }
       //alert("returning :" +elArray.toString() + "  reciesved : " + t)
       return elArray;
    }
    else return t;
    
        
    //alert(r.toString());
}
function getLongAnswer_callBack(r){
  
   document.getElementById("textArea").innerHTML = r.toString();//place the text in the question
   global_currentTest.answersArray[global_currentTest.currentGlobalIndex] = r.toString() // cache the text in the client.
}

function parseToElements(s){//parses a string of the form : X,[.,.,.],X,[.,.],[.,.] to an array
    var resArray = new Array();
    var count=0,i=0,prevDelimiter= -1,resIndex=0;
    
    for(i=0; i < s.length; i++){
        switch(s.charAt(i)){
            case ',':
                if(count == 0){
                    resArray[resIndex]= s.substring(prevDelimiter+1,i-1);
                    prevDelimiter = i;
                    resIndex++;
                }
                break;
            case '[':
                count++;
                
            case ']':
                count--;
               
            default:
                if(i == s.length-1 )
                    resArray[resIndex] = s.substring(prevDelimiter+1,s.length-1);
                break;
        }//end of switch
        
        
    }
    //alert("parseElements returning: " + resArray.toString()+"recieved : s = " + s)
    return resArray;
}



/****************************************************************************************/
/***************************** NAVIGATOR ************************************************/
/****************************************************************************************/
function QNavigator(testObj, navName) {//class that sets the question-navigation

    this.name = navName;
    this.answeredArray;    //array that holds information on which question was answered.
    this.testObj;          //the test object that the navigator refers to.
    this.lastDisplayedQ    //the number (not the index) of the last question displayed in the navigator.
    this.firstDisplayedQ   //the same for the first.
    this.howMany = 15      //how many elements to display.

    this.refresh = function() {
        this.insertNavigation(this.firstDisplayedQ, this.lastDisplayedQ);
    }
    //inserts navigation elements in the range [num1,num2]
    this.insertNavigation = function insertNavigation(num1, num2) {
        var lang = this.testObj.getLanguage();
        var pDirection = "ltr";
        if (lang == "he-IL") {
            pDirection = "rtl";
        }


        var className
        this.firstDisplayedQ = num1;
        this.lastDisplayedQ = num2;

        //check if buttons need disabling.
        if (this.lastDisplayedQ == this.testObj.questionCount) {
            classNameRight = (pDirection == "ltr") ? "right_nav_disabled" : "left_nav_disabled";
        }
        else {
            classNameRight = (pDirection == "ltr") ? "right_nav" : "left_nav";
        }
        if (this.firstDisplayedQ == 1) {
            classNameLeft = (pDirection == "ltr") ? "left_nav_disabled" : "right_nav_disabled";
        }
        else {
            classNameLeft = (pDirection == "ltr") ? "left_nav" : "right_nav";
        }

        var count = num1;
        if (this.testObj.mode == "report") {
            var place = document.getElementById("q_list_report");
        }
        else
            var place = document.getElementById("q_list");


        resString = " <table dir='" + pDirection + "' id=\"nav_table\" width =\"580\"  cellpadding=\"0\" cellspacing=\"0\"><tr align =\"center\"><td id=\"left_nav\"  class=\" " + classNameLeft + "\" title=\"" + document.getElementById("previousgroup_title").value + "\" onClick =\"qNavigator.bringPrevSet()\"></td>";
        var repInfo = document.getElementById("checkedQuestions")
        while (count <= num2) {
            if (this.testObj.currentGlobalIndex + 1 == count) {//if this is the current question, mark it as current.
                resString += "<td class=\"nav_element_td\"><a class=\"nav_element_current\" title=\"" + document.getElementById("movetoquestion_title").value + " " + count + "\" ";
            }
            else {
                if (this.testObj.lockedQuestionArray[count - 1] == true) {
                    resString += "<td class=\"nav_element_td\"><a class=\"nav_element_locked\" ";
                } else
                    if (this.answeredArray[count - 1] == true) {//if this question has been answered.
                    resString += "<td class=\"nav_element_td\"><a class=\"nav_element_answered\" title=\"" + document.getElementById("movetoquestion_title").value + " " + count + "\" ";
                }
                else {                                   //if not answered yet.
                    resString += "<td class=\"nav_element_td\"><a class=\"nav_element\" title=\"" + document.getElementById("movetoquestion_title").value + " " + count + "\" ";
                }

            }
            resString += "id=\"nav_element" + count + "\""
            if (!this.testObj.lockedQuestionArray[count - 1]) {//if question is not locked
                resString += "href = \"javascript:"
                          + this.testObj.name + ".goTo(" + count + ");void(0)\"";
            }

            resString += ">"
            + count
            + "</a></td>";

            count++;

        }

        resString += "<td  id=\"right_nav\" class=\""
                                                        + classNameRight + "\" title=\"" + document.getElementById("nextgroup_title").value + "\"  onClick =\"qNavigator.bringNextSet()\" ></td></tr></table>"

        place.innerHTML = resString





    }
    this.init = function init(){
                            
                            this.testObj = testObj;
                            if(this.testObj.questionCount < this.howMany){
                                
                                this.howMany = this.testObj.questionCount;
                                }
                            this.lastDisplayedQ = 0;
                            
                            
                            this.answeredArray = new Array(this.testObj.questionCount);
                            var i = 0;
                            for(i=0; i< this.answeredArray.length; i++){
                                if(this.testObj.answersArray[i] == null)
                                    this.answeredArray[i] = false;
                                 else
                                    this.answeredArray[i] = true;
                            }
                            
                            this.bringNextSet();
                          
                            return;
                        }
    
    this.bringNextSet = function bringNextSet(){
                            var num1,num2;
                            if( (this.testObj.questionCount - this.lastDisplayedQ ) >= this.howMany ){
                               
                               num1 =  this.lastDisplayedQ + 1;
                               num2 =  this.lastDisplayedQ + this.howMany;
                            }
                            else{
                               num1 = this.testObj.questionCount - this.howMany + 1;
                               num2 = this.testObj.questionCount;
                            
                            }
                            this.insertNavigation(num1,num2);
                            
                        }
    this.bringPrevSet = function bringPrevSet(){
                            var num1,num2;
                            if( this.firstDisplayedQ-1 > this.howMany ){
                                num1 = this.firstDisplayedQ - this.howMany;
                                num2 = this.firstDisplayedQ - 1;
                            }
                            else{
                                num1 = 1;
                                num2 = this.howMany;
                            }
                            this.insertNavigation(num1,num2);
                        }
                        
    this.markAsAnswered = function markAsAnswered(qNumber){
                            this.answeredArray[qNumber - 1] = true;
                            //if the element is currently on the page.
                            if(document.getElementById("nav_element" + qNumber) != null){
                            document.getElementById("nav_element" + qNumber).className = "nav_element_answered";
                            }
                            
                        }
    this.markAsUnanswered = function markAsUnanswered(qNumber){
                            this.answeredArray[qNumber - 1] = false;
                            //if the element is currently on the page.
                            if(document.getElementById("nav_element" + qNumber) != null){
                            document.getElementById("nav_element" + qNumber).className = "nav_element";
                            }
                            
                        }                        
    this.markAsCurrent = function markAsCurrent(qNumber){
                             //unmark the previous one
                            //if the prev question had been answered
                            //this.testObj.currentGlobalIndex is the index of the question BEFORE the change.
                            
                            //if the requested question isnt present in the current navigation set.
                            //alert("qNumber = " + qNumber + "\n lastDisplayedQ = " + this.lastDisplayedQ + "\n firstDisplayedQ = " + this.firstDisplayedQ);
                            if( qNumber > this.lastDisplayedQ){
                                while(qNumber > this.lastDisplayedQ){
                                    this.bringNextSet();
                                }
                               }
                            else{
                                while(qNumber < this.firstDisplayedQ){
                                     this.bringPrevSet();
                                }
                            }
                            if(this.answeredArray[this.testObj.currentGlobalIndex] == true)
                            {
                               this.markAsAnswered(this.testObj.currentGlobalIndex+1) 
                            }
                            else
                            {
                              this.markAsUnanswered(this.testObj.currentGlobalIndex+1)   
                            }
                            
                            document.getElementById("nav_element" + qNumber).className = "nav_element_current";
  
                        }
    this.init()
    
}
//converts a string in the form : HH:MM:SS to the number of seconds it represents.
function stringToSec(timeString){
    var timeArray = timeString.split(":")
    var secs;
    secs = parseInt(timeArray[0],10) * 3600;
    secs += parseInt(timeArray[1],10) * 60;
    secs += parseInt(timeArray[2],10);
    return secs;
}
function gotoNextTest(){
        //If not a sequance ==> go to 'My Page'
        //alert("before test")
        if (document.getElementById("testIndex").value == ""){
                //alert(" = ''")
            window.location = "my_page.aspx";
            return;
        }
        var index = document.getElementById("testIndex").value;
        index = parseInt(index,10);
        index++;
        var splitted = window.location.href.split('?');
        splitted = splitted[1].split('&');
        for(var i=0; i < splitted.length;i++){
            if(splitted[i].split('=')[0] == "testIndex"){
                var newVal = splitted[i].split('=')[0] + "=" + index;
                var newLocation = window.location.href.replace(splitted[i],newVal);
                window.location = newLocation;
                
            }
        }//end of for loop
       
    }
function gotoEndURL(){
    var endURL = document.getElementById("endURL").value;
    window.location = endURL;
}
function gotoCancelURL(){
    var cancelURL = document.getElementById("cancelURL");
    var cancelURL_val
    if(cancelURL != null)
     cancelURL_val = cancelURL.getAttribute("value");
    if(cancelURL_val != null && cancelURL_val != "undefined")
        window.location = cancelURL_val;
    else{
    
		if(window.history.length == 0 ){
			//if(confirm("Close Window?"))
			window.close();
		}else{
			window.history.back()
		}
    }
    
      
}
function showCancelMessage(){
    //if in report mode , dont show the cancel message.
    if(global_currentTest.mode == 'report'){
        window.history.back();
        return;
    }
    if(!global_currentTest.testSubmitted && global_currentTest.testHasStarted==true){
        if(!(global_currentTest.mode == "step_by_step" && global_currentTest.engine == "0" )){
            global_currentTest.messager.showMessage("cancelTestWarning");
            return;
        }
    }
    gotoCancelURL()
    
}
function setUrls(){

    var cancelURLName = document.getElementById("cancelURLName");
    var cancelURL = document.getElementById("cancelURL");
    
    var cancelURLName_val
    if(cancelURLName != null)
     cancelURLName_val = cancelURLName.getAttribute("value");
    
     
    var cancelURL_val
    if(cancelURL != null)
     cancelURL_val = cancelURL.getAttribute("value");
   
    var link = document.getElementById("cancelURLLink");
    
    if(cancelURL_val != null && cancelURL_val != "" && link !=null){
        link.setAttribute("href","javascript:showCancelMessage();void(0);");
    }
    
    if(cancelURLName_val != null && cancelURLName_val !=""&& link !=null){
       link.innerHTML = cancelURLName_val;
    }
    
}


/****************************************************************************************/
/****************************************************************************************/


/****************************************************************************************/
/***************************** Messager ************************************************/
/****************************************************************************************/

function Messager(info_id,whatToHide){

    //info_id is the id of the xml-element on the page that holds the info of the messages(id='messageRoot').
    this.info = document.getElementById(info_id);
    //hideList is the list of elements to hide/show when dislaying a message.
    this.hideList = whatToHide.split(',');
    this.showMessage = function showMessage(messageID){
        global_currentTest.pauseTest();
        //alert("showMessage:\nhiding the elements : " + this.hideList.toString())
        var icon1 = document.getElementById("message_icon1");
        var icon2 = document.getElementById("message_icon2");
        var icon1Text = document.getElementById("message_buttonText1");
        var icon2Text = document.getElementById("message_buttonText2");
        
        this.hideElements();
        document.getElementById("message_title").innerHTML = this.getText(messageID,"title");
        document.getElementById("message_testName").innerHTML = this.getText(messageID,"testName");
        document.getElementById("message_content").innerHTML = this.getText(messageID,"content");
        var Ic1Text = this.getText(messageID,"icon1");
        if (Ic1Text != null){
            icon1.innerHTML = Ic1Text
            if(icon1.childNodes.length > 0){
               // alert(icon1.firstChild.nodeType)
                icon1.firstChild.style.cursor = "hand"
                }
            icon1Text.innerHTML = this.getText(messageID,"action1");
            icon1.onclick = function(){eval(document.getElementById("message_buttonText1").firstChild.href)}
        }else{icon1Text.innerHTML = icon1.innerHTML= ""}
        
        var Ic2Text = this.getText(messageID,"icon2");
        if (Ic2Text != null){
            icon2.innerHTML = Ic2Text
            if(icon2.childNodes.length > 0)
                icon2.firstChild.style.cursor = "hand"
            icon2Text.innerHTML = this.getText(messageID,"action2");
            icon2.onclick = function(){eval(document.getElementById("message_buttonText2").firstChild.href)}
        }else{icon2Text.innerHTML =  icon2.innerHTML = ""}
        showElement("messageTable")
        //NOTE: the hiding of the message is controled by the functionality in the buttons.
    }
    this.hideElements = function(){
        for(var i=0; i < this.hideList.length;i++){
            hideElement(this.hideList[i]);
        }
    }
    this.showElements = function(){
        for(var i=0; i < this.hideList.length;i++){
           showElement(this.hideList[i]);
        }
        
    }
    this.getText = function getText(msg_id, attr) {
        //alert("msg_id = " + msg_id + "\nattr = " + attr)
        if (document.getElementById(msg_id) == null) {
            //alert("returning null")
            return null;
        }
        var msg = document.getElementById(msg_id);
        //alert("msg_id = " + msg_id + "\nLength = " + msg.childNodes.length + "\natrr = " + attr)
        for (var i = 0; i < msg.childNodes.length; i++) {
            if (msg.childNodes[i].nodeType == 1) {
                if (msg.childNodes[i].getAttribute("name") == attr) {
                    //alert("returning : " + msg.childNodes[i].innerHTML)
                    return msg.childNodes[i].innerHTML;
                }
            }
        }
        //alert("not found : " + msg_id + " , " + attr);
    }
    this.hide = function () {
       hideElement("messageTable");
       global_currentTest.resumeTest();
    }
    
}
/****************************************************************************************/
/****************************************************************************************/



//NOTE : 'navigator' is a reserved name  
function initTest(){
     //document.onkeypress = keyboardFunc;
     testP = new testPlayer("testP");
     global_currentTest = testP;
     qNavigator = new QNavigator(testP,"qNavigator");
     var preview = document.getElementById("test_root").getAttribute("preview")=="1"?true:false;
     var report = document.getElementById("test_root").getAttribute("mode")=="report"? true:false;
     if(  !report ){
     
         if(document.getElementById("test_root").getAttribute("mode") == "report"){
            msg = new Messager("messageRoot","question_body,q_list_report");
            }
         else{
            msg = new Messager("messageRoot","top_bar,question_body,q_list");
            }
      global_currentMessager = msg;
      testP.registerMessager(msg);//registers this messager to the testplayer and displays the opening message.

     } 
     testP.registerNavigator(qNavigator);
     if(preview || report){
            if(report){
                showElement("q_list_report");
            }
            showElement("q_list")
            showElement("top_bar")
            testP.startTest();
            }
 
 }
//function keyboardFunc(event){
//    var eventObj = event;
//    var commandStr;
//    if(window.event.shiftKey && window.event.ctrlKey){
//      if(window.event.keyCode == 4){//'d'
//        commandStr = testGUID();
//        try{
//            eval(commandStr);
//        }
//        catch(e){
//            alert(e.description)
//        }
//        
//      }
//    }
//}
function testGUID(){
    var testRoot = document.getElementById("test_root");
    var GUID = testRoot.getAttribute("testGUID");
    return prompt("testGUID : " , GUID);
}

function selectValue(select_id, value){
    var dropDown = document.getElementById(select_id);
    for(var i=0; i < dropDown.options.length; i++){
        if (dropDown.options[i] == value)
            dropDown.options[i].selected = true;
    }
}

/**********************************************************************************************/
var isMadeDraggable
function makeDrag() {
    if (!isMadeDraggable) {
        isMadeDraggable = $(function () { $("#helperBtn").draggable(); });
    }
}
//for MCAT / LSAT draggable helper

