var FIELDS = [["Name", "name" ],
              ["Rank", "rank" ],
              ["Grade", "grade"],
              ["Job title", "job_title"],
              ["Social Security Account Number (SSAN)", "ssan"],
              ["Personnel Identifier (PID)", "pid", "*"],
              ["Employee Number", "empnum", "*"],
              ["Military occupational Specialty (MOS)", "mos"],
              ["Unit/Activity assigned to", "activity"]];

function reportTrainee (doc, data)
{
  var tbody = doc.getElementById ("rpthdr_body");
  for (var i = 0; i < FIELDS.length; ++i) {
    var fld = FIELDS[i];
    var val = data[fld[1]];
    if (val) {
      var tr = tbody.appendChild (doc.createElement ("tr"));
      var td = tr.appendChild (doc.createElement ("td"));
      td.className = "trainee_field_title";
      td.appendChild (doc.createTextNode(fld[0] + ":"));
      td = tr.appendChild (doc.createElement ("td"));
      td.className = "trainee_field_value";
      td.appendChild (doc.createTextNode(val));
    }
  }
}

function createCell (doc, tr, className, html, colspan)
{
  var td;
  td = doc.createElement ("td");
  
  if (colspan) {
    td.colSpan = colspan;
  }

  td = tr.appendChild (td);
  td.className = className;
  td.innerHTML = html;
}


function createIndent (doc, tr, indent)
{
  var td;
  td = doc.createElement ("td");
  td.colSpan = indent;
  
  td = tr.appendChild (td);
  td.className = "indent";
}

function reportAnswer (doc, tbody, answer, index)
{
  var tr = tbody.appendChild (doc.createElement ("tr"));
  createIndent (doc, tr, 2);
  createCell (
    doc, tr, "answer",  
    "Answer: <b>(" + index + ") " + answer.text + "</b>",
    1
  );
}


function reportQuestion (doc, tbody, question, tskindex, index)
{
  var tr = tbody.appendChild (doc.createElement ("tr"));

  createIndent (doc, tr, 1);
  createCell (doc, tr, "question",
              "<i>Question " + tskindex + "." + index + ":</i> <b>" 
              + question.text + "</b>", 2);

  var answers = question.answers;
  var selectedAnswers = question.selectedAnswers;
  for (var i = 0; i < selectedAnswers.length; ++i) {
    var answerNumber = selectedAnswers[i];
    reportAnswer (doc, tbody, answers[answerNumber], answerNumber + 1);
  }
}


function reportTask (doc, tbody, task, index)
{
  var tr = tbody.appendChild (doc.createElement ("tr"));

  createCell (
    doc, tr, "task", 
    "<i>Task " + index + ":</i> <b>" 
    + task.title + "</b>",
    3
  );

  var questions = task.questions;
  for (var i = 0; i < questions.length; ++i)
    reportQuestion (doc, tbody, questions[i], index, i + 1);
}


function reportTest (doc, test)
{
  if (!test)
    throw "No test passing information available";

  var tbody = doc.getElementById ("rptbody");
  var tr = tbody.appendChild (doc.createElement ("tr"));
  var html = 
    "<img src='" + (test.passed ? "success.gif" : "unsuccess.gif") + "'>"
  + "<i>Test: </i> <b>" + test.title + " - "
  + (test.passed ? "<span class=passed>PASSED</span>" : "<span class=failed>FAILED</span>")
  + "</b>";
  createCell (doc, tr, "test", html, 4);

  var tasks = test.tasks;
  for (var i = 0; i < tasks.length; ++i) {
    reportTask (doc, tbody, tasks[i], i + 1);
    tbody.appendChild (doc.createElement ("br"));
  }
}


function generateReport (doc)
{
  try {
    reportTrainee (doc, window.opener.TRAINEE_DATA);
    reportTest (doc, window.opener.TEST_PASSED);
  }
  catch (x) {
    alert (x);
    throw x;
  }
}


function openReportWindow (doc)
{
  var baseUrl = window.top.BASE_URL;
  var baseWin = window.top.opener.top;
  var id = 'twin';
  var features = baseWin._makeFeatures (440, 440, false, false);

  var dialog = new _ModalDialog (
    id, window,
    function (state) {
      if (state) {
        window.TRAINEE_DATA = state;
        window.TEST_PASSED  = window.top.TEST;
        window.open (baseUrl + "training_report.html");
      }
    }
  );

  dialog._open (
    baseWin._preparedWindowUrl (baseUrl + 'trainee_form.html', id),
    null,
    features
  );
}

function _ModalDialog (id, win, onclose)
{
  this._open = function (url, args, features)
  {
    if (win.showModalDialog)
      onclose (win.showModalDialog (url, args, features));
    else {
      win.document._getArgs = function() { return args; };
      var dialog = win.open (url, "dialog_" + id, features);
      dialog.addEventListener (
        "load",
        function () {
          dialog.onunload = function() 
          {
            onclose (this.returnValue); 
            delete win.document._getArgs;
          };
        },
        false
      );
    }
  }
}

/*
window.top.TEST_PASSED = new Object();
var _t = window.top.TEST_PASSED;
_t.title = "A test that <i>rocks</i>";

_t.tasks = new Array();
for (var i = 0; i < 5; ++i) {

  var t = new Object();
  _t.tasks.push(t);
  t.title = "We have carefully designed these questions for you";
  t.questions = new Array();

  var questions = _t.tasks[i].questions;
  for (var j = 0; j < 5; ++j) {
    var q = new Object();
    questions.push (q);
    q.text = "A question that has number <u>" + (j + 1) + "</u>";

    q.answers = new Array();
    for (var k = 0; k < 5; ++k) {
      var answer = new Object();
      q.answers.push (answer);
      answer.text = ("An answer that <u>"
                      + (k % 2 == 0 ? "sucks" : "nuts") + "</u>");
    }
    q.selectedAnswers = new Array();
    q.selectedAnswers[0] = Math.floor (Math.random() * 5);
  }
}
*/