// ------------------
// Default page

// Main page: execute a test
function proceed(){
  document.entryForm.submit();
};
function beginTest(){
  document.entryForm.title.value = document.beginTestForm.title.value;
  document.entryForm.forename.value = document.beginTestForm.forename.value;
  document.entryForm.surname.value = document.beginTestForm.surname.value;
  if (document.beginTestForm.sex[0].checked) { document.entryForm.sex.value = '1'; }
  if (document.beginTestForm.sex[1].checked) { document.entryForm.sex.value = '2'; }
  document.entryForm.overrides1.value = 'true';
  proceed();
}

// Main page: choose sample questionnaire
function sampleQuestionnaire(){
  if (lateBrowser()) {
    location.href = 'q1.html';
  } else {
    location.href = 'q1.html';
  }
};

// Main page: send report code
function submitRepcode(){
  document.repCode.submit();
};

// ------------------
// Sample questionnaires

var questionnaire = new questionnaireType;
var currentQuestion = 0;
function submitQA(){
  document.questionsForm.submit();
};
function returnDefault(){
  location.href='default.asp';
};
function PreQFormat()
{
  var ret_string = '';
  ret_string = '<font face="arial,helvetica" size="1" color="black">';
  ret_string += '<span><b>';
  return ret_string;
}
function PostQFormat()
{
  return '</b></span></font>';
}
function checkRepeats(index,column)
{
  if (column == 0)
  {
    document.sampleQuestions.qleast[index].checked = false;
  }
  else
  {
    document.sampleQuestions.qmost[index].checked = false;
  };
};

function changeQuestion()
{
  var i;
  var most;
  var least;
  var charCode = 'ABCD';
  var error = '';
  var lastQuestion = 2;
  var formState = false;
  var tmp_string = '';
  var tmp_string2 = '';
  var bName = navigator.appName;
  var bVer = parseInt(navigator.appVersion);
  var NS4 = (bName == "Netscape" && bVer >= 4);
  var IE4 = (bName == "Microsoft Internet Explorer" && bVer >= 4);
  // Assess 'Most' and 'Least'
  most = '?';
  least = '?';
  for (i = 0; i <= 3; i++)
  {
    if (document.sampleQuestions.qmost[i].checked)
    {
      most = charCode.charAt(i);
    };
    if (document.sampleQuestions.qleast[i].checked)
    {
      least = charCode.charAt(i);
    };
  };
  // Validate
  if ((most == '?') || (least == '?'))
  {
    error = 'You must select a \'Most\' and a \'Least\' answer before proceeding.';
  };
  if ((most == least) && (error == ''))
  {
    error = 'The answer you choose for \'most\' and a \'least\' cannot be the same.';
  };
  if (error != '')
  {
    window.alert(error);
    return false;
  }
  else
  {
    // Store Response
    if (currentQuestion > 0)
    {
      eval('document.sampleQuestions.q'+currentQuestion+'most.value = most');
      eval('document.sampleQuestions.q'+currentQuestion+'least.value = least');
    };
    // Clear Existing Radios
    for (i = 0; i <= 3; i++)
    {
      document.sampleQuestions.qmost[i].checked = false;
      document.sampleQuestions.qleast[i].checked = false;
    };
    // Update Display
    if (currentQuestion == lastQuestion){
      currentQuestion = -1;
    };
      currentQuestion++;
      tmp_string = '<h4>';
      tmp_string += 'EXAMPLE QUESTION';
      tmp_string += '</h4>';
      tmp_string2 = /%S/;
          caption.innerHTML = tmp_string;
          qtexta.innerHTML = PreQFormat() + questionnaire[currentQuestion][0] + PostQFormat();
          qtextb.innerHTML = PreQFormat() + questionnaire[currentQuestion][1] + PostQFormat();
          qtextc.innerHTML = PreQFormat() + questionnaire[currentQuestion][2] + PostQFormat();
          qtextd.innerHTML = PreQFormat() + questionnaire[currentQuestion][3] + PostQFormat();
    return false;
  };
};
function question(a,b,c,d)
{
  this[0] = a;
  this[1] = b;
  this[2] = c;
  this[3] = d;
};
function questionnaireType()
{
  this[0] = new question('Adopting a dominant attitude to others','Influencing other people\'s ideas','Remaining steady and calm at all times','Complying with instructions and regulations');
  this[1] = new question('Behaving compassionately towards others','Persuading others to your point of view','Showing modesty in describing your achievements','Producing original ideas');
  this[2] = new question('Receiving attention from other people','Working together with others to achieve an aim','Standing up for your rights','Showing affection in personal relationships');
}

function showMessage(){
  window.alert('After completing the actual questionnaire, you should press this \'Questionnaire Completed\' button to submit your answers.');
};

// ------------------
// Utilities

function lateBrowser() {
  var late = false;
  var userAgent = navigator.userAgent;
  userAgent = userAgent.toLowerCase();
  if (userAgent.indexOf('firefox') != -1) { late = true; }
  if (userAgent.indexOf('safari') != -1) { late = true; }
  if (userAgent.indexOf('msie') != -1) {
    late = true;
    if (userAgent.indexOf('msie 4.') != -1) { late = false; }
    if (userAgent.indexOf('msie 3.') != -1) { late = false; }
    if (userAgent.indexOf('msie 2.') != -1) { late = false; }
    if (userAgent.indexOf('msie 1.') != -1) { late = false; }
  }
  return late;
}
