    
var currentQuestion = 0;
var answersArray = [];
var correctAnswers = 0;
var extReg = new RegExp("(.*)\\.([^.]*)$","ig")
var fileReg = new RegExp(".*\\\\(.+\\.[^.]+)$","ig")

function removeABC(file){
return(file.replace(/^(.*)_[a-z]$/i,"$1"));
}

function getExt(file){
if (file.indexOf(".")==-1){return("FOLDER");}
var output="$2";
return(file.replace(extReg,output))
}

function getStem(file){
if (file.indexOf(".")==-1){return("FOLDER");}
var output="$1";
return(file.replace(extReg,output))
}

function showQTI(file){
var fileStem=getStem(file);
var qFolder=removeABC(getStem(file));
var defaultWidth="100%";
var defaultHeight="100%";
    
    var so = new SWFObject("viewer/viewer.swf", "mymovie", defaultWidth, defaultHeight, "7", "#c0e6f8");//#C0E6F8
    so.addVariable("specificQ", fileStem );
    so.addVariable("specificFldr", "questions/"+qFolder+"/");
    so.addVariable("outsideDMS", 1);
    so.addVariable("noTextBoxesPlease", 1);
    so.addVariable("showFeedback", 1);
//    so.addVariable("sendOutputViaGetUrl", 1);
    
    //so.addVariable("launchCalculatorViaJS", 1);
    //so.addVariable("allFilesLowerCase", 1);//UNIX only
    so.addParam("allowScriptAccess", "sameDomain");
    so.write("qtiDiv_"+currentQuestion);

//    so.addVariable("mode", 1);//review mode untested

}

function openCalculator(calcType){
//alert(calcType);
}

function flashHandler(resultStr){

//record data
answersArray.push(resultStr);

// check result string and report if correct or incorrect to user
    if (resultStr.indexOf("score=1")>=0){
    shout("Correct");
    correctAnswers+=1;
    }
    
    else {
    shout("Incorrect");
    }

// go to next question
currentQuestion+=1;
openNextQuestion();
    
}

function shout(a){

//alert(a)

}

function endTest(){

//showAllQuestions();
var message

    if (questionSet.length>1){
    message="<br />You answered "+correctAnswers+" question"+(correctAnswers==1?"":"s")+" correctly out of "+questionSet.length;
    }
    else {
    message=(correctAnswers==1?"You answered correctly":"You answered incorrectly");
    }

$("commentDiv_0").className="";
showBoxes();
hideLastQuestion();

$("resultsDiv").className="";
$("resultsDiv").innerHTML=message;

    if (questionSet.length>1){
    $("buttonDiv").innerHTML='<br /><input type="button" value="Start test again" onclick="javascript:location.reload();void(0);" class="but1" />';
    }
    else {
    $("buttonDiv").innerHTML='<br /><input type="button" value="Try question again" onclick="javascript:location.reload();void(0);" class="but1" />';
    }

}

function openNextQuestion(){

// end test if no more questions
    if (questionSet.length<=currentQuestion){

    endTest();
    return;

    }


    var fileName=questionSet[currentQuestion]+".xml";

    hideLastQuestion();
    
    //show new question div
    $("qtiDiv_"+currentQuestion).className="qtidiv shown";
    $("commentDiv_"+currentQuestion).className="commentdiv shown";
//    $("commentDiv_"+currentQuestion).innerHTML="Question "+(currentQuestion+1)+" of "+(questionSet.length)+":<br />";

    showBoxes();
    
    showQTI(fileName);
    

}

function hideLastQuestion(){

    //hide last question
    if (currentQuestion!=0){
    $("qtiDiv_"+(currentQuestion-1)).className="hidden";
    $("commentDiv_"+(currentQuestion-1)).className="hidden";
    }
    
}

function showBoxes(){

    var blockHTML=new StringBuffer;
    
    for (var i=0,j=questionSet.length;i<j;i++){
    
    blockHTML.append('<div class="box ');

        if (typeof(answersArray[i])!="undefined"){
        blockHTML.append(answersArray[i].indexOf("score=1")>=0?"boxcorrect":"boxincorrect");
        }
        else {
        blockHTML.append("incomplete");
        }

    blockHTML.append('">Q');
    blockHTML.append(i+1);
    blockHTML.append('</div>');
    
    }
    
    blockHTML.append('<div clear="both;"></div><br />')
    
    $("boxDiv").innerHTML=blockHTML.toString();
    
}


function setUpDivs(){

var divHTML = new StringBuffer

if (questionSet.length>1){
    //box div
    divHTML.append('<div class="boxdiv" id="boxDiv"></div>');
}
else {
    divHTML.append('<div class="boxdiv" id="boxDiv" style="display:none;"></div>');
}

    //results div
    divHTML.append('<div class="hidden resultsdiv" id="resultsDiv"></div>');
    
    for (var i=0,j=questionSet.length;i<j;i++){

    // write and 2 empty divs for each question - one for comments, one for the question itself (note that this does not preserve the user's work in Flash as the flash item resets when made visible again)
        divHTML.append('<div class="hidden commentdiv" id="commentDiv_');
        divHTML.append(i);
        divHTML.append('"></div>');
        divHTML.append('<div class="hidden qtidiv" id="qtiDiv_');
        divHTML.append(i);
        divHTML.append('"></div>');

    }

    //results div
//    divHTML.append('<div class="" id="buttonDiv"><input type="button" title="Reload question with new values" value="Reload question" onclick="javascript:reloadQuestion();void(0);" class="but1" /></div>');
    
$("contentDiv").innerHTML=divHTML.toString();


}

function reloadQuestion(){
var fileName=questionSet[currentQuestion]+".xml";
showQTI(fileName);
}

function showAllQuestions(){

    for (var i=0,j=questionSet.length,correct;i<j;i++){
    correct=(answersArray[i].indexOf("score=1")>=0?true:false)
        $("commentDiv_"+i).className="commentdiv shown "+(correct?"correct":"incorrect");
        $("commentDiv_"+i).innerHTML="<br /><hr width='80%' /><br />Question "+(i+1)+ ": "+(correct?"CORRECT":"INCORRECT")+"<br />";
        
        $("qtiDiv_"+i).className="qtidiv shown";
    }

}

function loader(){

setUpDivs();
openNextQuestion();
    
}

//assign onload
(function(){
var onload=window.onload||function(){};
document.body.setAttribute('onload',null);
window.onload=function(){onload.apply(this,arguments);loader();}
})();
