﻿
//---------------------------------------
//PopUp
//---------------------------------------
function displayWindow(url, width, height) {
    var Win = window.open(url, "displayWindow", 'width=' + width + ',height=' + height + ',resizable=1,scrollbars=yes,menubar=no,status=yes');
}


//---------------------------------------
//FLASH VIDEO
//---------------------------------------


function MM_CheckFlashVersion(reqVerStr,msg){
  with(navigator){
    var isIE  = (appVersion.indexOf("MSIE") != -1 && userAgent.indexOf("Opera") == -1);
    var isWin = (appVersion.toLowerCase().indexOf("win") != -1);
    if (!isIE || !isWin){  
      var flashVer = -1;
      if (plugins && plugins.length > 0){
        var desc = plugins["Shockwave Flash"] ? plugins["Shockwave Flash"].description : "";
        desc = plugins["Shockwave Flash 2.0"] ? plugins["Shockwave Flash 2.0"].description : desc;
        if (desc == "") flashVer = -1;
        else{
          var descArr = desc.split(" ");
          var tempArrMajor = descArr[2].split(".");
          var verMajor = tempArrMajor[0];
          var tempArrMinor = (descArr[3] != "") ? descArr[3].split("r") : descArr[4].split("r");
          var verMinor = (tempArrMinor[1] > 0) ? tempArrMinor[1] : 0;
          flashVer =  parseFloat(verMajor + "." + verMinor);
        }
      }
      // WebTV has Flash Player 4 or lower -- too low for video
      else if (userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 4.0;

      var verArr = reqVerStr.split(",");
      var reqVer = parseFloat(verArr[0] + "." + verArr[2]);
  
      if (flashVer < reqVer){
        if (confirm(msg))
          window.location = "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
      }
    }
  } 
}



function MM_CheckFlashVersion(reqVerStr,msg){
  with(navigator){
    var isIE  = (appVersion.indexOf("MSIE") != -1 && userAgent.indexOf("Opera") == -1);
    var isWin = (appVersion.toLowerCase().indexOf("win") != -1);
    if (!isIE || !isWin){  
      var flashVer = -1;
      if (plugins && plugins.length > 0){
        var desc = plugins["Shockwave Flash"] ? plugins["Shockwave Flash"].description : "";
        desc = plugins["Shockwave Flash 2.0"] ? plugins["Shockwave Flash 2.0"].description : desc;
        if (desc == "") flashVer = -1;
        else{
          var descArr = desc.split(" ");
          var tempArrMajor = descArr[2].split(".");
          var verMajor = tempArrMajor[0];
          var tempArrMinor = (descArr[3] != "") ? descArr[3].split("r") : descArr[4].split("r");
          var verMinor = (tempArrMinor[1] > 0) ? tempArrMinor[1] : 0;
          flashVer =  parseFloat(verMajor + "." + verMinor);
        }
      }
      // WebTV has Flash Player 4 or lower -- too low for video
      else if (userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 4.0;

      var verArr = reqVerStr.split(",");
      var reqVer = parseFloat(verArr[0] + "." + verArr[2]);
  
      if (flashVer < reqVer){
        if (confirm(msg))
          window.location = "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
      }
    }
  } 
}

//---------------------------------------
//TEST
//---------------------------------------


// Define global variables and arrays
var questionIndex = 0;
var checker = true;
var keeper = new Array();
var performance = new Array('Avez-vous vraiment lu le roman ?', 'Cela fait-il plus d\'un an que vous avez lu le roman ?', 'Bien, vous avez survolé le roman.', 'Pas mal : vous avez retenu les grandes lignes du roman.', 'Très bien ! Vous avez une bonne mémoire et du discernement.', 'Excellent ! Vous êtes doté d\'une mémoire exceptionnelle.', 'Fameux ! Votre capacité de rétention des détails est prodigieuse.', 'Incroyable ! Vous connaissez le roman mieux que son auteur !!!');
var correctAnswers = 0;
var jsAbility;
var preventer = 1;
var spawn;

// For resetting all variables and arrays for a retake
function itemReset() {
    questionIndex = 0;
    keeper = new Array();
    correctAnswers = 0;
    preventer = 1;
    jsAbility = null;
}

// Administer the test and record the answers
function startQuiz(openStatus, whetherOrNot) {
    checker = openStatus;
    if (whetherOrNot < questionIndex) { busted(); return; }
    if (checker) {
        spawn = open('', '', 'width=550,height=350,scrollbars=yes');
        !checker;
    }

    if (questionIndex == killers.length) { gradeTest(); return; }

    // Separate each array element to form the questions and answr options
    var question = killers[questionIndex].substring(0, killers[questionIndex].indexOf('$$'));
    var optionA = killers[questionIndex].substring(killers[questionIndex].indexOf('$$') + 2, killers[questionIndex].indexOf('%%'));
    var optionB = killers[questionIndex].substring(killers[questionIndex].indexOf('%%') + 2, killers[questionIndex].indexOf('^^'));
    var optionC = killers[questionIndex].substring(killers[questionIndex].indexOf('^^') + 2, killers[questionIndex].indexOf('&&'));
    var optionD = killers[questionIndex].substring(killers[questionIndex].indexOf('&&') + 2, killers[questionIndex].length);

    // Write the questons and answrs options to the new window
    spawn.document.clear();
    spawn.document.open();
    spawn.document.writeln('<html><head><title>Quiz sur Gunni le gauche</title></head>');
    spawn.document.writeln('<BODY BGCOLOR="\#D7C87A\">');
    spawn.document.writeln('<H2>Question ' + (questionIndex + 1) + '</H2>');
    spawn.document.writeln('<FORM>');
    spawn.document.writeln('<B>' + question + '</B><br/><br/>');
    spawn.document.writeln('<INPUT TYPE=RADIO NAME=\"answer\" VALUE=\"a\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionA + '<br/>');
    spawn.document.writeln('<INPUT TYPE=RADIO NAME=\"answer\" VALUE=\"b\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionB + '<br/>');
    spawn.document.writeln('<INPUT TYPE=RADIO NAME=\"answer\" VALUE=\"c\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionC + '<br/>');
    spawn.document.writeln('<INPUT TYPE=RADIO NAME=\"answer\" VALUE=\"d\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionD + '<br/>');
    spawn.document.writeln('</BODY></html>');
    spawn.document.close();

    // Increment variables for the next question
    questionIndex++;
    preventer++;
}

function gradeTest() {
    // Increment the variables for the last time
    questionIndex++;
    preventer++;

    // Compare student answers with correct answers
    for (var i = 0; i < killers.length; i++) {
        if (keeper[i] == answers[i]) {
            correctAnswers++;
        }
    }

    // Determine a ranking according to number of corrrect answers
    jsAbility = performance[Math.ceil((correctAnswers / killers.length) * performance.length - 1)];

    // Print the test results
    printResults();
}

// Print the questions, answer options, and other info
function printResults() {
    spawn.document.clear();
    spawn.document.open();
    spawn.document.writeln('<html><head>');
    spawn.document.writeln('<title>Quiz sur Gunni le gauche - Résultats</title>');
    spawn.document.writeln('</head><BODY BGCOLOR=\"#D7C87A\">');
    spawn.document.writeln('<FONT FACE=\"Arial\">');
    spawn.document.writeln('<H2>Résultat ' + correctAnswers + '/' + killers.length + '</H2>');
    spawn.document.writeln('<B>&nbsp; ' + jsAbility + '</B><br/>');
    spawn.document.writeln('<br/><br/><FONT SIZE=\"4\">Les réponses : </FONT><br/><br/>');

    for (var i = 0; i < killers.length; i++) {
        spawn.document.writeln('\n\n<B>Question ' + (i + 1) + '</B><br/>');
        spawn.document.writeln(killers[i].substring(0, killers[i].indexOf('$$')));
        spawn.document.writeln('<br/><br/>\n<FONT SIZE=\"-1\">a. ' + killers[i].substring((killers[i].indexOf('$$') + 2), killers[i].indexOf('%%')) + '<br/>');
        spawn.document.writeln('b. ' + killers[i].substring(killers[i].indexOf('%%') + 2, killers[i].indexOf('^^')) + '<br/>');
        spawn.document.writeln('c. ' + killers[i].substring(killers[i].indexOf('^^') + 2, killers[i].indexOf('&&')) + '<br/>');
        spawn.document.writeln('d. ' + killers[i].substring(killers[i].indexOf('&&') + 2, killers[i].length) + '<br/></FONT>');

        // Determine if the student answered each question appropriately, and display accordingly
        if (keeper[i] == answers[i]) {
            spawn.document.writeln('<B><I><FONT COLOR=\"GREEN\">Vous avez répondu correctement.</FONT></I></B>\n<br/><br/><br/>');
        }
        else { spawn.document.writeln('<B><I><FONT COLOR=\"RED\">La bonne réponse est : ' + answers[i] + '\n<br/><br/><br/></FONT></I></B>'); }
    }


}

function busted() {
    alert('Désolé, vous avez seulement un chance par question.');
    spawn.history.forward();
    spawn.focus();
}