
function exam_header_column(include_result) {
  include_result = include_result || 0;
  tableRow();
  textCell("Month");
  textCell("Year");
  textCell("Awarding Body");
  textCell("Subject");
  textCell("Level/Qual");
  if (include_result==1) { textCell("Result,<br>Grade,<br>Mark or<br>Band"); }
}

function setYear(index) {
  x = new Date();
  if (document.getElementById('txtUserSubExamYear' + index).value == '') { document.getElementById('txtUserSubExamYear' + index).value = x.getUTCFullYear(); }
}

function exams(name, total) {
// known names are "UserSub" (from submit_exams_x.asp), and "Actual" and "New" (from SMUCB_UG_App_Form.html)
  for (i=1 ; i<=total; i++) {
    document.write('<tr>');
    if (name == 'UserSub') {
      popUp('cbo' + name + 'ExamMonth' + i, MonthList, "setYear(" + i + ");");
    } else {
      popUp('cbo' + name + 'ExamMonth' + i, MonthList);
    }
    inputCell('txt' + name + 'ExamYear' + i, 4, 4, "checkYear_2params('" + 'txt' + name + 'ExamYear' + i + "', validateExamYears, '" + name + "', " + total + "); \" onKeyPress = \"return preventKeyPress(event, digitEntered);");
    popUp('cbo' + name + 'ExamAB' + i, examBoardList, "boardChange(" + i + ", '" + name + "');");
    inputCell('txt' + name + 'ExamSub' + i, 20, 50, "\" onKeyPress = \"return preventKeyPress(event, nameCharEntered);");
    popUp('cbo' + name + 'ExamLevel' + i, examLevelList, "levelChange(" + i + ", '" + name + "');");
    if ((name == 'Actual') || (name == 'UserSub')) popUp('cbo' + name + 'ExamGrade' + i, examGradeList);
  }
  document.write('</table><font size=2>Make a selection from the Awarding Body list to populate the Qualification list.');
  if (name != "New") document.write('<br>Make a selection from the Level/Qual list to populate the Grade list.<br><br>');
}

function boardChange(index, name) {
  var combo = document.getElementById('cbo' + name + 'ExamAB' + index);
  var myRecSet = selectDistinct(sortRecordSet(applyFilter(db,'Board==' + combo.options[combo.selectedIndex].text), sortByColumn1),'Level');
  var myArray = makeArray(myRecSet, 0, 1);
  repopulateCombo('cbo' + name + 'ExamLevel' + index, myArray);
  levelChange(index, name);
}

function levelChange(index, name) {
  if ((name == 'Actual') || (name == 'UserSub')) {
    var boardCombo = document.getElementById('cbo' + name + 'ExamAB' + index);
    var levelCombo = document.getElementById('cbo' + name + 'ExamLevel' + index);
// a blank board combo is not a problem because the blank exists as a menu item, but when the level is blank, it is actually empty, so we must avoid accessing the level menu in this circumstance
    if (levelCombo.selectedIndex==-1) {
      var myRecSet = Array();
    } else {
      var myRecSet = selectDistinct(applyFilter(applyFilter(db,'Board==' + boardCombo.options[boardCombo.selectedIndex].text),'Level==' + levelCombo.options[levelCombo.selectedIndex].text),'Grade');
    }
    var myArray = makeArray(myRecSet, 0, 1);
    repopulateCombo('cbo' + name + 'ExamGrade' + index, myArray);
    var valueIndex = findValueIndex('cbo' + name + 'ExamGrade' + index, 'X');
    var gradeCombo = document.getElementById('cbo' + name + 'ExamGrade' + index);
    gradeCombo.selectedIndex = valueIndex;
  }
}

function examCompletion(controlBaseName, nofExams) {
  for (i=1; i<=nofExams; i++) {

// if ((any field other than year is filled in) && (any field other than year is not filled in))
// NOTE THAT EXAM LEVEL AND RESULT DO NOT HAVE A BLANK ENTRY IN POSITION 0 SO THE PREDICATE IS DIFFERENT

    if (controlBaseName=="New") {
      bExamGradeIsBlank = false;
      bExamGradeIsNotBlank = false;
    } else {
      bExamGradeIsBlank = document.getElementById('cbo' + controlBaseName + 'ExamGrade' + i).selectedIndex==-1;
      bExamGradeIsNotBlank = document.getElementById('cbo' + controlBaseName + 'ExamGrade' + i).selectedIndex>-1;
    }
    some_field_is_blank = (document.getElementById('cbo' + controlBaseName + 'ExamMonth' + i).selectedIndex==0 || 
      document.getElementById('txt' + controlBaseName + 'ExamYear' + i).value=="" ||
      document.getElementById('cbo' + controlBaseName + 'ExamAB' + i).selectedIndex==0 || 
      document.getElementById('txt' + controlBaseName + 'ExamSub' + i).value=="" || 
      document.getElementById('cbo' + controlBaseName + 'ExamLevel' + i).selectedIndex==-1 || 
      bExamGradeIsBlank);

    some_field_is_not_blank = (document.getElementById('cbo' + controlBaseName + 'ExamMonth' + i).selectedIndex>0 ||
      document.getElementById('txt' + controlBaseName + 'ExamYear' + i).value!="" ||
      document.getElementById('cbo' + controlBaseName + 'ExamAB' + i).selectedIndex>0 || 
      document.getElementById('txt' + controlBaseName + 'ExamSub' + i).value!="" || 
      document.getElementById('cbo' + controlBaseName + 'ExamLevel' + i).selectedIndex>-1 || 
      bExamGradeIsNotBlank);

    incomplete = some_field_is_blank && some_field_is_not_blank;

    if (incomplete) {
      alert("In row number " + i + ", all fields must be completed, or the row cleared entirely, before the form can be submitted.");
      return false;
    }
  }
  return true;
}

function examCompletion_also_retired_now_year_is_not_an_exception(controlBaseName, nofExams) {
// this version refers to the exam result self-submission form. which has been decommissioned
  for (i=1; i<=nofExams; i++) {
//    if (document.getElementById('txt' + controlBaseName + 'ExamYear' + i).value=="") {
//      alert("In row number " + i + ", the year must be completed, or the row cleared entirely, before the form can be submitted.");
//      return false;
//    }

// if ((any field other than year is filled in) && (any field other than year is not filled in))
// note that exam level and result do not have a blank entry in position 0 so the predicate is different

    some_field_is_blank = (document.getElementById('cbo' + controlBaseName + 'ExamMonth' + i).selectedIndex==0 || 
      document.getElementById('cbo' + controlBaseName + 'ExamAB' + i).selectedIndex==0 || 
      document.getElementById('txt' + controlBaseName + 'ExamSub' + i).value=="" || 
      document.getElementById('cbo' + controlBaseName + 'ExamLevel' + i).selectedIndex==-1 || 
      document.getElementById('cbo' + controlBaseName + 'ExamGrade' + i).selectedIndex==-1);

    year_is_blank = document.getElementById('txt' + controlBaseName + 'ExamYear' + i).value==""; 

    some_field_is_not_blank = (document.getElementById('cbo' + controlBaseName + 'ExamMonth' + i).selectedIndex>0 ||
      document.getElementById('cbo' + controlBaseName + 'ExamAB' + i).selectedIndex>0 || 
      document.getElementById('txt' + controlBaseName + 'ExamSub' + i).value!="" || 
      document.getElementById('cbo' + controlBaseName + 'ExamLevel' + i).selectedIndex>-1 || 
      document.getElementById('cbo' + controlBaseName + 'ExamGrade' + i).selectedIndex>-1);

// note that the year field is a special case; in the exam result self-submission form, it is preloaded
    if (controlBaseName == 'UserSub' && !year_is_blank && !some_field_is_blank) {
      incomplete = false;
    } else {
      incomplete = (some_field_is_blank || year_is_blank) && (some_field_is_not_blank && !year_is_blank);
    }

    if (incomplete) {
      alert("In row number " + i + ", all fields must be completed, or the row cleared entirely, before the form can be submitted.");
      return false;
    }
  }
  return true;
}


function examCompletion_was_new_but_retired(controlBaseName, nofExams) {
// when self-submit exams came back on the scene, the old version was reinstated
  for (i=1; i<=nofExams; i++) {

// note that exam level and result do not have a blank entry in position 0 so the predicate is different

    some_field_is_blank = (document.getElementById('cbo' + controlBaseName + 'ExamMonth' + i).selectedIndex==0 || 
      document.getElementById('txt' + controlBaseName + 'ExamYear' + i).value=="" ||
      document.getElementById('cbo' + controlBaseName + 'ExamAB' + i).selectedIndex==0 || 
      document.getElementById('txt' + controlBaseName + 'ExamSub' + i).value=="" || 
      document.getElementById('cbo' + controlBaseName + 'ExamLevel' + i).selectedIndex==-1 || 
      document.getElementById('cbo' + controlBaseName + 'ExamGrade' + i).selectedIndex==-1);

    some_field_is_not_blank = (document.getElementById('cbo' + controlBaseName + 'ExamMonth' + i).selectedIndex>0 ||
      document.getElementById('txt' + controlBaseName + 'ExamYear' + i).value!="" ||
      document.getElementById('cbo' + controlBaseName + 'ExamAB' + i).selectedIndex>0 || 
      document.getElementById('txt' + controlBaseName + 'ExamSub' + i).value!="" || 
      document.getElementById('cbo' + controlBaseName + 'ExamLevel' + i).selectedIndex>-1 || 
      document.getElementById('cbo' + controlBaseName + 'ExamGrade' + i).selectedIndex>-1);

    incomplete = (some_field_is_blank) && (some_field_is_not_blank);


    if (incomplete) {
      alert("In row number " + i + ", all fields must be completed, or the row cleared entirely, before the form can be submitted.");
      return false;
    }
  }
  return true;
}
