
function TrainingTest (id, pscore, title, variants)
{
  this.id = id;
  this.pscore = pscore;
  this.title = title;
  this.variants = variants;

  this.selectVariant = TrainingTest_selectVariant;

  return this;
}

function TrainingTest_selectVariant()
{
  this.variantNumber = Math.floor (Math.random() * this.variants.length);
  var variant = this.variants[this.variantNumber];
  this.tasks = variant.tasks;
  this.currentTask = 0;
  return variant;
}



function TrainingVariant (id, title, tasks)
{
  this.id = id;
  this.title = title;
  this.tasks = tasks;

  return this;
}

function findTaskNumber (tasks, id)
{
  for (var i = tasks.length - 1; i >= 0; --i) {
    var task = tasks[i];
    if (task.id == id)
      return i;
  }
  return -1;
}



function TrainingTask (id, title, url, questions)
{
  this.id = id;
  this.title = title;
  this.url = url;
  this.questions = questions;

  return this;
}


function TrainingQuestion (id, single, critical, title, text, answers)
{
  this.id = id;
  this.single = single;
  this.critical = critical;
  this.title = title;
  this.text = text;
  this.answers = answers;

  this.correctAnswers = [];
  var answersLength = answers.length;
  for (var i = 0; i < answersLength; ++i) {
    if (answers[i].correct == "true")
      this.correctAnswers.push (i);
  }

  return this;
}


function TrainingAnswer (id, correct, text, comment)
{
  this.id = id;
  this.correct = correct;
  this.text = text;
  this.comment = comment;

  return this;
}




function trainingStartTest()
{
  if (!window.top._testing) {
  var test = getTest();
    test.selectVariant();

    var tasks = test.tasks;
    var tasksLength = tasks.length;
    for (var i = 0; i < tasksLength; ++i) {
      var task = tasks[i];
      task.isAnswered = false;
    }

    window.top.DOC_URL  = getBaseUrl (window.location.href);
    window.top.BASE_URL = getBaseUrl (window.top.location.href);
    window.open (window.top.BASE_URL + "test_frames.html", "test", "height=480,width=640,menubar=0,scrollbars=0,toolbar=0,resizable=1,status=1,titlebar=1");
  }
}


function showComment (id)
{
  var doc = window.document;
  var inputNodes = doc.getElementsByTagName ("input");
  if (inputNodes) {
    var length = inputNodes.length;
    for (var i = 0; i < length; ++i) {
      var inputNode = inputNodes[i];
      if (inputNode.name == id) {
        var commentNode = doc.getElementById (inputNode.id + "comment");
        if (commentNode) {
          with (commentNode.style) {
            if (inputNode.checked)
              display = "block";
            else
              display = "none";
          }
        }
      }
    }
  }
}


function trainingFinishTask()
{
  var doc = window.document;
  var task = doc._task;
  if (!readResults (task, doc))
    return;

  calcScore (getTest());
  openReportWindow (doc);
}


function trainingNextTask()
{
  var doc = window.document;
  var task = doc._task;
  if (!readResults (task, doc))
    return;

  ++getTest().currentTask;
  var frame = window.top.document.getElementById ("task_content");
  frame.src = window.top.DOC_URL + doc._nextTask.url;
}


function readResults (task, doc)
{
  if (!task.isAnswered) {
  var questions = task.questions;
  var questionsLength = questions.length;
  for (var i = 0; i < questionsLength; ++i) {
    var question = questions[i];
    question.selectedAnswers = [];

    var answers = question.answers;
    var answersLength = answers.length;
    for (var j = 0; j < answersLength; ++j) {
      var answerNode = doc.getElementById (answers[j].id);
      if (answerNode.checked)
        question.selectedAnswers.push (j);
    }

    if (question.selectedAnswers.length == 0) {
      alert ("You have to answer all questions");
      return false;
    }
  }
  disableTask (task, doc);
  task.isAnswered = true;
  }
  return true;
}

function restoreTaskState (task, doc)
{
  var questions = task.questions;
  var questionsLength = questions.length;
  for (var i = 0; i < questionsLength; ++i) {
    var question = questions[i];

    var answers = question.answers;
    var selectedAnswers = question.selectedAnswers;
    var selectedAnswersLength = selectedAnswers.length;
    for (var j = 0; j < selectedAnswersLength; ++j) {
      var answerNode = doc.getElementById (answers[selectedAnswers[j]].id);
      answerNode.checked = true;
    }
  }
}

function disableTask (task, doc)
{
  var questions = task.questions;
  var questionsLength = questions.length;
  for (var i = 0; i < questionsLength; ++i) {
    var question = questions[i];

    var answers = question.answers;
    var answersLength = answers.length;
    for (var j = 0; j < answersLength; ++j) {
      var answerNode = doc.getElementById (answers[j].id);
      answerNode.disabled = true;
    }
  }
}



function calcScore (test)
{
  var tasks = test.tasks;
  var rawScore = calcRawScore (tasks);
  var questionsNumber = getQuestionsNumber (tasks);
  var actualScore = (rawScore / questionsNumber) * 100;

  var passedCritical = passedCriticalQuestions (tasks);

  test.actualScore = actualScore;
  test.passed = passedCritical && actualScore >= test.pscore;
}

function calcRawScore (tasks)
{
  var rawScore = 0;
  var tasksLength = tasks.length;
  for (var i = 0; i < tasksLength; ++i) {
    var questions = tasks[i].questions;
    var questionsLength = questions.length;
    for (var j = 0; j < questionsLength; ++j) {
      if (isQuestionAnsweredCorrect (questions[j]))
        ++rawScore;
    }
  }
  return rawScore;
}

function isQuestionAnsweredCorrect (question)
{
  var correctAnswers = question.correctAnswers;
  var selectedAnswers = question.selectedAnswers;
  if (correctAnswers.length != selectedAnswers.length)
    return false;

  var length = correctAnswers.length;
  for (var i = 0; i < length; ++i) {
    if (correctAnswers[i] != selectedAnswers[i])
      return false;
  }

  return true;
}

function getQuestionsNumber (tasks)
{
  var questionsNumber = 0;
  var tasksLength = tasks.length;
  for (var i = 0; i < tasksLength; ++i)
    questionsNumber += tasks[i].questions.length;
  return questionsNumber;
}

function passedCriticalQuestions (tasks)
{
  var tasksLength = tasks.length;
  for (var i = 0; i < tasksLength; ++i) {
    var questions = tasks[i].questions;
    var questionsLength = questions.length;
    for (var j = 0; j < questionsLength; ++j) {
      var question = questions[j];
      if (question.critical == "true" && !isQuestionAnsweredCorrect (question))
        return false;
    }
  }
  return true;
}


function getTest()
{
  var test = window.top.TEST;
  if (!test) {
    test = window.top.opener.top.TEST;
    window.top.TEST = test;
  }
  return test;
}


function getBaseUrl (url)
{
  var i = url.lastIndexOf ("\\");
  if (i == -1)
    i = url.lastIndexOf ("/");
  return i == -1 ? "/" : url.substr (0, i + 1);
}


function trainingTestLoad() {}

function trainingTaskLoad()
{
  var doc = window.document;
  var test = getTest();
  var tasks = test.tasks;

  var taskId = doc.body.id;
  var taskNumber = findTaskNumber (tasks, taskId);
  var task = tasks[taskNumber];
  test.currentTask = taskNumber;

  if (task.isAnswered) {
    restoreTaskState (task, doc);
    disableTask (task, doc);
  }
  doc._task = task;

  var finishTaskButton = doc.getElementById ("finishButton");
  if (finishTaskButton) {
    if (taskNumber == tasks.length - 1) {
      finishTaskButton.value = "Finish";
      finishTaskButton.onclick = trainingFinishTask;
    }
    else {
      finishTaskButton.value = "Next Task";
      finishTaskButton.onclick = trainingNextTask;

      doc._nextTask = tasks[taskNumber + 1];
    }
  }
}


function trainingTestUnload() {}
function trainingTaskUnload() {}
