
//---------------------------------------------------------------------//
// CHECK DOCUMENT ENCODING MATCHES THE REST OF THE SITE BEFORE SAVING! //
//---------------------------------------------------------------------//

// VARIABLE DEFINITIONS
var TimerId = 0;
var TimerId_L2 = 0;
if ((document.location.pathname.lastIndexOf('/') == document.location.pathname.lastIndexOf('/hd_')) ||
		(document.location.pathname.lastIndexOf('/') == document.location.pathname.lastIndexOf('/site_map'))) {
	var MaxL1Menus = 8;
}
else {
	var MaxL1Menus = 7;
}
var MaxL2Menus = 7;

// Create array to hold names of submenus in a tree to indicate their relationship

var WhichMenuArray = new Array();

WhichMenuArray['ma2'] = [2, 0];
WhichMenuArray['ma23'] = [2, 3];
WhichMenuArray['ma24'] = [2, 4];
WhichMenuArray['ma3'] = [3, 0];
WhichMenuArray['ma32'] = [3, 2];
WhichMenuArray['ma33'] = [3, 3];
WhichMenuArray['ma34'] = [3, 4];
WhichMenuArray['ma4'] = [4, 0];
WhichMenuArray['ma43'] = [4, 3];
WhichMenuArray['ma5'] = [5, 0];
WhichMenuArray['ma52'] = [5, 2];
WhichMenuArray['ma53'] = [5, 3];
WhichMenuArray['ta2'] = [6, 0];
if (MaxL1Menus > 7) {
	WhichMenuArray['ma6'] = [7, 0];
}

var SubMenuArray = new Array(MaxL1Menus);

SubMenuArray[0] = new Array(MaxL2Menus);
SubMenuArray[1] = new Array(MaxL2Menus);
SubMenuArray[2] = new Array(MaxL2Menus);
SubMenuArray[3] = new Array(MaxL2Menus);
SubMenuArray[4] = new Array(MaxL2Menus);
SubMenuArray[5] = new Array(MaxL2Menus);
SubMenuArray[6] = new Array(MaxL2Menus);
if (MaxL1Menus > 7) {
	SubMenuArray[7] = new Array(MaxL2Menus);
}

SubMenuArray[2][0] = 'sm2';
SubMenuArray[2][3] = 'sm23';
SubMenuArray[2][4] = 'sm24';
SubMenuArray[3][0] = 'sm3';
SubMenuArray[3][2] = 'sm32';
SubMenuArray[3][3] = 'sm33';
SubMenuArray[3][4] = 'sm34';
SubMenuArray[4][0] = 'sm4';
SubMenuArray[4][3] = 'sm43';
SubMenuArray[5][0] = 'sm5';
SubMenuArray[5][2] = 'sm52';
SubMenuArray[5][3] = 'sm53';
SubMenuArray[6][0] = 'tm2';
if (MaxL1Menus > 7) {
	SubMenuArray[7][0] = 'sm6';
}

/*****************************************************************************/
/* Arrays used to convert numeric representations of the day of the week and */
/* month of the year into their textual versions.                            */
/*****************************************************************************/
var day_to_word_array = new Array('Sunday',
                                  'Monday',
                                  'Tuesday',
                                  'Wednesday',
                                  'Thursday',
                                  'Friday',
                                  'Saturday');
                                  
var month_to_word_array = new Array('January',
                                    'February',
                                    'March',
                                    'April',
                                    'May',
                                    'June',
                                    'July',
                                    'August',
                                    'September',
                                    'October',
                                    'November',
                                    'December');


// If the page has opened in a window without a name, give it a name
// so that we can open pages back in the same window from child windows
// opened remotely from this window using the openwindow function.
if (window.parent.name == "") {
  window.parent.name = "MAIN_WINDOW";
  var name_set="YES";
}

// ***********************************************************

// FUNCTIONS

// Functions to show and hide submenus


function ShowSubMenu(callers_id) {
	// Get the submenu numbers for this menu item link
	num = WhichMenuArray[callers_id][0];
	snum = WhichMenuArray[callers_id][1];
	
  // If there are outstanding timeouts, clear them
  clearTimeout(TimerId);
  clearTimeout(TimerId_L2);
	
	// Get the SubMenuArray indexes that correspond to this menu entry link

  HideOtherSubMenus(num, snum);
  document.getElementById(SubMenuArray[num][0]).style.display = 'block';
 
  // If snum is not 0 we need to make the corresponding Level 2 SubMenu visible too
  if (snum) {
    document.getElementById(SubMenuArray[num][snum]).style.display = 'block';
  }  
}
	
function HideSubMenu(callers_id) {
	// Get the submenu numbers for this menu item link
	num = WhichMenuArray[callers_id][0];
	snum = WhichMenuArray[callers_id][1];

  MenuName = SubMenuArray[num][0];
  TimerId = setTimeout("document.getElementById(MenuName).style.display = 'none' ", 600);
//  document.write(SubMenuArray[num][snum]);
	
  // If we've just come off a menu item makes a Level 2 SubMenu appear we'd better set a timer  
  // to hide the Level 2 SubMenu.  NB: If we've moved onto a different item in the Level 1 SubMenu 
  // the Level 2 SubMenu will re-appear instantaneously thanks to HideOtherSubMenus being called.
  if (snum) {
    MenuNameL2 = SubMenuArray[num][snum];
    TimerId_L2 = setTimeout("document.getElementById(MenuNameL2).style.display = 'none' ", 600);
  }  
}
	
function HideOtherSubMenus(num, snum) {
  // Scan through all Level 1 SubMenus, hiding all those that don't correspond to 'num'
  for (i=0; i < MaxL1Menus; i++) { 
    if ((SubMenuArray[i][0]) && (i != Number(num))) {
      document.getElementById(SubMenuArray[i][0]).style.display = 'none';   
    }
    // Check for Level 2 SubMenus that need closing
    for (ii=1; ii < MaxL2Menus; ii++) {
      if ((SubMenuArray[i][ii]) && 
      		(document.getElementById(SubMenuArray[i][ii])) && 
      		(ii != Number(snum))) {
        // Level 2 menu exists, and is not the one to keep visible, so hide it
        document.getElementById(SubMenuArray[i][ii]).style.display = 'none';
      }
    }
  }
}

// Function to go back to previous page
function goback() {
  if (navigator.appName.substring(0,9) == "Microsoft") {
    history.go(-1);
  }
  else {
    back();
  }
}


// Function to pop up an alert window if the browser being used is too old a version 
// to warn the visitor that the site would look better with a more recent browser.
function CheckBrowser() {
  var isDOM = (document.getElementById ? true : false);
  var TheyKnow = false;
  if (document.cookie) {
    cookieString = document.cookie;
    cookieArray = cookieString.split("=");
    if (cookieArray[0] == "TheyKnowC" && cookieArray[1] == "true" ) {
      TheyKnow = true;
    }
  } 
  if (!isDOM && !TheyKnow) {
    alert("This website has been designed for a more recent browser version than yours. \n                  \nTo optimise your viewing experience we recommend that you visit the Search Associates \nHarry Deelman branch website with an updated version of your browser.  Alternatively, \nuse the Site Map to navigate this website. \n\n(To not see this message again during this session make sure you allow cookies)");
    document.cookie = "TheyKnowC=true";
  }
}


// Function to open up an new browser window for links to external websites to open in
function openwindow(target, href) {
	var external_window;
  
  if (target.indexOf("SEARCH") == 0) {
		external_window = window.open(href, target, "location,menubar,status,toolbar,scrollbars,resizable,width=805,height=530");	
	}
	
	else if (target.indexOf("EXT") == 0) {
		external_window = window.open(href, target, "location,menubar,status,toolbar,scrollbars,resizable,width=800,height=400");
	}
	
	else if (target.indexOf("NOVOTEL") == 0) {
		external_window = window.open(href, target, "location,menubar,status,toolbar,scrollbars,resizable,width=1000,height=450");
	}
	
	else if (target.indexOf("SCI") == 0) {
		external_window = window.open(href, target, "location,menubar,status,toolbar,scrollbars,resizable,width=935,height=450");
	}
	
	else if (target.indexOf("CANDEDIT") == 0) {
		external_window = window.open(href, target, "location,menubar,status,toolbar,scrollbars,resizable,width=800,height=600");
	}
	
	else if (target.indexOf("HOW_TO") == 0) {
		external_window = window.open(href, target, "toolbar,scrollbars,resizable,width=550,height=350");
	}
	
	else {
		external_window = window.open(href, target, "location,menubar,status,toolbar,scrollbars,resizable,width=700,height=400");
	}
	
	external_window.focus();
}


// Function to open up an new browser window for forms on external websites to open in
function openFormExternally(target, caller_form) {
	var external_window;
  
  if (target.indexOf("SEARCH") == 0) {
		external_window = window.open("", target, "location,menubar,status,toolbar,scrollbars,resizable,width=805,height=530");
	}
	
	else {
		external_window = window.open("", target, "location,menubar,status,toolbar,scrollbars,resizable,width=700,height=400");
	}
	
  external_window.focus();
  caller_form.submit();
  return (external_window);
} /* End of openFormExternally function. */


// Function to check School Recruiters have filled in some login information
// before hitting the submit button.
function RecFormValidate(target, caller_form) {
	if (caller_form.school_username.value == "") {
		alert("You must enter a username.");
		caller_form.school_username.focus();
		return (false);
	}
	
	if (caller_form.school_password.value == "") {
		alert("You must enter a password.");
		caller_form.school_password.focus();
		return (false);
	}
	
	external_window = openFormExternally(target, caller_form);

  document.location.reload();
  caller_form.reset();
  external_window.focus();
	
	return (false);
} /* End of RecFormValidate function. */


// Function to check Candidates have filled in some login information
// before hitting the submit button.
function CandFormValidate(target, caller_form) {
	if (caller_form.username.value == "") {
		alert("You must enter both a username and password.");
		caller_form.username.focus();
		return (false);
	}
	
	if (caller_form.password.value == "") {
		alert("You must enter both a username and password.");
		caller_form.password.focus();
		return (false);
	}

	if (!(caller_form.available[0].checked) && 
	    !(caller_form.available[1].checked)) {
		alert("Please indicate your availability");
		return (false);
	}
	
	external_window = openFormExternally(target, caller_form);
  
  document.location.reload();
  caller_form.reset();
  external_window.focus();

	return (false);
} /* End of CandFormValidate function. */


// Function to check that the necessary fields have been filled-in in 
// Evaluation Form selection form before it is submitted to the .com site.
function EvalFormCheck(target, caller_form) {
 	if (caller_form.eval_type.selectedIndex == 0) 
        {
        alert("You must select an evaluation form type.");
        caller_form.eval_type.focus();
        return (false);
        }

	if (caller_form.teacher.value == "" ) 
        {
        alert("You must enter the Candidate's name.");
        caller_form.teacher.focus();
        return (false);
        }
        
	external_window = openFormExternally(target, caller_form);

  document.location.reload();
  caller_form.reset();
  external_window.focus();

	return (false);
} /* end of EvalFormCheck function */


// Function that calculates and writes in the fees in the Fair School Registration
// Form based on the number of extra interviewers passed in to it.
function ReCalculateFees(extra_int_num, sch_cost, extra_int_cost) {
	// alert(extra_int_num);
	
  /* Set up the variables we will use later on in this function.             */
	var int_fee_line_id = 'int_fee_line';
	var total_fee_amount_id = 'total_fee_amount';
	var hidden_rec_fee_total_id = 'hidden_rec_fee_total';
	var new_fee_line_html = '';
		
  /* Calculate the new fee for additional interviewers and total fee and     */
  /* store them in variables for use later on.                               */
	var extra_int_fee = Number(extra_int_num) * Number(extra_int_cost);
	var total_fee = extra_int_fee + Number(sch_cost);
	
  /* Write the new additional interviewer(s) fee line's HTML, or leave it    */
  /* blank if there are no additional interviewers.                          */
	if (Number(extra_int_num) > 0) {
		var new_fee_line_html = '<br /><span class="form-row-sub-heading" id="int_fee_hd">Plus:</span>';
		new_fee_line_html += ' US$&nbsp;' + extra_int_fee + '&nbsp; for ' + extra_int_num + ' additional interviewer';
		
		if (Number(extra_int_num) > 1) {
			new_fee_line_html += 's';
		}
	}
	
  /* Replace the additional interviewer(s) fee line with the new HTML string */
  /* and the fee total with the new total fee.                               */
	document.getElementById(int_fee_line_id).innerHTML = new_fee_line_html;
	document.getElementById(total_fee_amount_id).innerHTML = total_fee;
	
  /* For safety's sake, we'll set the hidden input element's value and       */
  /* default value both to the new total_fee.                                */
	document.getElementById(hidden_rec_fee_total_id).defaultValue = total_fee;
	document.getElementById(hidden_rec_fee_total_id).value = total_fee;
	// alert(document.getElementById(hidden_rec_fee_total_id).defaultValue);
	// alert(document.getElementById(hidden_rec_fee_total_id).value);
	
} /* end of ReCalculateFees function */


// Function that adjusts the note regarding the addition of the details 
// of the as yet undefined interviewers.
function ChangeAddIntNote(changes_ok_till) {
  // alert("In ChangeAddIntNote(" + changes_ok_till +")");
	var add_int_note_id = "add_int_note";
	var int_num_id = "rec_int_num_id";
	var new_add_int_note = '';
	
  /* Quit this function without doing anything if the school hasn't yet      */
  /* specified how many interviewers they plan to send to the fair.          */
  if (document.getElementById(int_num_id).value == "") {
	  return;
  }
  
  /* Find out how many interviewer's details fields are currently displayed  */
  /* and subtract this from the number of interviewers the school plans to   */
  /* send, in order to obtain the number of interviewers for who no details  */
  /* have yet been defined.                                                  */
	var rec_int_names_array = document.getElementsByName('rec_int_name[]');
	var curr_num_int_details = Number(rec_int_names_array.length);
	var int_num_expected = Number(document.getElementById(int_num_id).value);
	
	var num_undef_int = int_num_expected - curr_num_int_details;
	
	/* Write the new note about adding the remaining interviewers which will   */
	/* appear below the interviewer detail fields.                             */
	if (num_undef_int > 0) {
		new_add_int_note = '<p>You have until ' + changes_ok_till;
		new_add_int_note +=	' to tell us who your other ';
		if (num_undef_int > 1) {
			new_add_int_note += num_undef_int + ' interviewers';
		}
		else {
			new_add_int_note += 'interviewer';
		}
		new_add_int_note += ' will be.&nbsp; However, if you already know who ';
		if (num_undef_int > 1) {
			new_add_int_note += 'they\'ll ';
		}
		else {
			new_add_int_note += 'he/she will ';
		}
		new_add_int_note += 'be, please add their details to this form now.</p>';
	}
	else if (num_undef_int < 0) {
		new_add_int_note = '<p><strong>NB:</strong> You currently have more ';
		new_add_int_note += 'interviewer fields than the number of ';
		new_add_int_note += 'interviewers you have said your school expects ';
		new_add_int_note += 'to send to this Fair.</p>';
	}
	
	/* Assuming that the new and the old additional interviewer notes are NOT  */
	/* BOTH empty strings, replace the old one with the new one.               */
	if ((new_add_int_note != '') || 
			(document.getElementById(add_int_note_id).innerHTML != '')) {
		document.getElementById(add_int_note_id).innerHTML = new_add_int_note;
	}
} /* end of ChangeAddIntNote function */


// Function that does all the changes that are dependent on what the number
// of interviewers a school intends to send to the fair is.
function ProcessIntNum(int_num_input, sch_cost, extra_int_cost, changes_ok_till) {
	var int_num = int_num_input.value;
	// alert("int_num = " + int_num);
	
	/* Check whether the value obtained is a number in digits.  If it is not   */
	/* a number, including if it is blank alert the user to specify a number   */
	/* in digits and don't proceed with the rest of the function.              */
	if (isNaN(int_num) || (int_num == '')) {
		alert("Please specify the number of interviewers your school expects to send to this Fair in digits.");
	  int_num_input.focus();
		return (false);
	}
	
	/* Reject any numbers lower than 1, because each school must have at least */
	/* one interviewer at the fair.                                            */
	if (int_num < 1) {
		alert("Your school must send at least 1 interviewer to attend the Fair.");
	  int_num_input.focus();
		return (false);
	}
	
	/* Subtract 1 from the number of interviewers that the school is sending   */
	/* to the fair to obtain the number of extra interviewers they will have   */
	/* to pay for separately.                                                  */
	var extra_int_num = Number(int_num) - 1; 

	/* Now adjust the note about adding the details of the additional          */ 
	/* interviewers and update the fee to match the number extra interviwers.  */
	ChangeAddIntNote(changes_ok_till);	
	ReCalculateFees(extra_int_num, sch_cost, extra_int_cost);	
} /* end of ProcessIntNum function */


// Function that checks that the interviewer number has been set by the 
// user and alerts them to do so if they haven't yet. 
function CheckIntNumIsSet() {
  if (document.getElementById("rec_int_num_id").value == "") {
  	var alert_message = "Please fill in the number of interviewers your school expects to ";
  	alert_message += "send to this Fair, in the space provided, before filling in any ";
  	alert_message += "of your interviewers\' details.";
	  alert(alert_message);
	  document.getElementById('rec_int_num_id').focus();
	  return (false);
  }
} /* end of CheckIntNumIsSet function */

// Function that adds the fields to submit a further interviewer's details in
// the School Fair Registration Form
function AddInterviewer(interviewer_num, changes_ok_till) {
  /* Set the variables we are going to use later on.                         */
	var div_id = "interviewer_fields";
	var paragraph_id = "add_interviewer";
	var new_interviewer_num = Number(interviewer_num) + 1;

  /* Obtain the full current contents of the 'interviewer_fields' division   */
  /* to which we will append the blank form fields which will allow the      */
  /* school to fill in the details of an additional interviewer.             */
	var div_content = document.getElementById(div_id).innerHTML;
	// alert(div_content);

  /* Set the variable containing the HTML that writes the fields             */
  /* into which the school can write an additional interviewer's details.    */
	var extra_int_html = '<!-- start int_details_' + interviewer_num + ' -->\n';
	extra_int_html += '<table id="int_table_' + interviewer_num + '" class="boxed-item">\n';
	extra_int_html += '  <tr>\n';
	extra_int_html += '    <td>\n';
	extra_int_html += '    <p class="int-sect-heading">Interviewer:</p>\n';
	extra_int_html += '    <span id="int_title_' + interviewer_num + '_hd" class="form-row-heading">Title (Mr/Mrs/Ms/Dr)</span>\n';
	extra_int_html += '<input type="text" name="rec_int_title[]" id="rec_int_title_' + interviewer_num;
	extra_int_html += '" title="Interviewer Title" onfocus="CheckIntNumIsSet();" ';
	extra_int_html += 'onchange="ChangeDefaultValue(this.id);" value="" size="6" /><br />\n';
	extra_int_html += '    <span id="int_name_' + interviewer_num + '_hd" class="form-row-heading">First Name</span>\n';
	extra_int_html += '<input type="text" name="rec_int_name[]" id="rec_int_name_' + interviewer_num;
	extra_int_html += '" title="Interviewer First Name" onfocus="CheckIntNumIsSet();" ';
	extra_int_html += '" onchange="ChangeDefaultValue(this.id);" value="" size="40" /><br />\n';
	extra_int_html += '    <input type="hidden" name="rec_int_alias[]" value="" />\n';
	extra_int_html += '    <span id="int_surname_' + interviewer_num + '_hd" class="form-row-heading">Surname</span>\n';
	extra_int_html += '<input type="text" name="rec_int_surname[]" id="rec_int_surname_' + interviewer_num;
	extra_int_html += '" title="Interviewer Surname" onfocus="CheckIntNumIsSet();" ';
	extra_int_html += '" onchange="ChangeDefaultValue(this.id);" value="" size="40" /><br />\n';
	extra_int_html += '    <span id="int_position_' + interviewer_num;
	extra_int_html += '_hd" class="form-row-heading">Position In School</span>\n';
	extra_int_html += '<input type="text" name="rec_int_position[]" id="rec_int_position_' + interviewer_num;
	extra_int_html += '" title="Interviewer Position" onfocus="CheckIntNumIsSet();" ';
	extra_int_html += '" onchange="ChangeDefaultValue(this.id);" value="" size="40" /><br />\n';
	extra_int_html += '    <input type="hidden" name="rec_int_role[]" value="" />\n';
	extra_int_html += '    <span id="int_email_' + interviewer_num + '_hd" class="form-row-heading">E-mail</span>\n';
	extra_int_html += '<input type="text" name="rec_int_email[]" id="rec_int_email_' + interviewer_num;
	extra_int_html += '" title="Interviewer E-mail" onfocus="CheckIntNumIsSet();" ';
	extra_int_html += '" onchange="ChangeDefaultValue(this.id);" value="" size="40" /><br />\n';
	extra_int_html += '    <input type="hidden" name="rec_int_room[]" value="" />\n';
	extra_int_html += '    <a href="#" class="float-right" onclick="javascript:RemoveInterviewer(';
	extra_int_html += interviewer_num + ', \'' + changes_ok_till;
	extra_int_html += '\'); return false;">Remove This Interviewer</a></td></tr></table>\n';
	extra_int_html += '<!-- end int_details_' + interviewer_num + ' -->';

  /* Append the additional interviewer's blank fields to the existing ones.  */
  var new_div_content = div_content + extra_int_html;
	// alert(new_div_content);

  /* Set the 'interviewer_fields' span contents to the new HTML, which now   */
  /* includes the blank fields into which the school can write an            */
  /* additional interviewer's details.                                       */
  document.getElementById(div_id).innerHTML = new_div_content;

  /* Re-write the 'Add An Additional Interviewer' link so that it will pass  */
  /* in the right interviewer index number to this function next time.       */
  var new_add_int_paragraph = '<a href="#" onclick="javascript:AddInterviewer(';
	new_add_int_paragraph += new_interviewer_num + ', \'' + changes_ok_till;
	new_add_int_paragraph += '\'); return false;">Add An Additional Interviewer\'s Details</a>'
	document.getElementById(paragraph_id).innerHTML = new_add_int_paragraph;

  /* Call ChangeAddIntNote to adjust the note regarding adding additional    */
  /* interviewers' details in view of the new number of interviewer details. */
	ChangeAddIntNote(changes_ok_till);
	
	/* Call CheckIntNumIsSet to remind user to set the number of interviewers  */
	/* before filling in any interviewer details.                              */
	CheckIntNumIsSet();	

} /* End of AddInterviewer function. */


// Function that removes an interviewer's details from the School Fair
// Registration Form
function RemoveInterviewer(rem_int_num, changes_ok_till) {
	// alert("in RemoveInterviewer(" + rem_int_num + ', ' + changes_ok_till + ")");
	
  /* Set the variables we are going to use later on.                         */
	var div_id = "interviewer_fields";
	
  /* Figure out how many interviewer's details are currently in the form and */
  /* what the new total will be after this interviewer's details have been   */
  /* removed from the form.                                                  */
	var rec_int_names_array = document.getElementsByName('rec_int_name[]');
	var int_num_before = Number(rec_int_names_array.length);
	// alert("int_num_before = " + int_num_before);
	var int_num_after = int_num_before - 1;
	// alert("int_num_after = " + int_num_after);
	
  /* Note that the form must always contain the details of at least one      */
  /* interviewer, so if the new number of interviewers (were this removal    */
  /* completed) is equal to zero we must not go through with the removal.    */
  /* Pop a warning message to the user and exit the function.                */
	if (int_num_after == 0) {
		alert("You must register at least one interviewer's details");
		return;
	}
	
  /* Set the variables containing start and end strings of of the section    */
  /* of HTML we need to remove from the 'interviewer_fields' division.       */
	var rem_str_start = '<!-- start int_details_' + rem_int_num + ' -->';
	var rem_str_end = '<!-- end int_details_' + rem_int_num + ' -->';
	// alert("rem_str_start = " + rem_str_start + ";  rem_str_end = " + rem_str_end + ";   rem_str_end.length = " + rem_str_end.length);
	
  /* Obtain the full current contents of the 'interviewer_fields' division,  */
  /* from which we will be removing the form fields corresponding to the     */
  /* interviewer whose index number has been passed in.                      */
  /* We lower-case this string because the property names in HTML markups    */
  /* may have already been lower-cased by the browser, automatically.        */
	var div_content = document.getElementById(div_id).innerHTML;
	// alert("div_content = " + div_content);
	var lc_div_content = div_content.toLowerCase();
	// alert("lc_div_content = " + lc_div_content);

  /* Search for the position of the start and end of the section             */
  /* corresponding to the interviewer whose details we need to remove.       */
  /* To avoid problems due to browsers changing the case of HTML markups     */
  /* we search for a lower-cased string within a lower-case string.          */
	var start_index = lc_div_content.indexOf(rem_str_start.toLowerCase());
	var end_index = lc_div_content.indexOf(rem_str_end.toLowerCase()) + rem_str_end.length;
	// alert("start_index = " + start_index + ";  end_index = " + end_index);
	
	if ((start_index == -1) || (end_index == (rem_str_end.length - 1))) {
		alert("This feature does not work with your browser. \nPlease use a more recent browser version to fill-in this form.");
		return (false);
	}

  /* Obtain the string between the start and end indeces of the section      */
  /* corresponding to the interviewer whose details we need to remove        */
	var rem_string = div_content.substring(start_index, end_index);
	// alert("rem_string = " + rem_string);

  /* Replace the details of the interviewer we are removing with an empty    */
  /* string (ie: with nothing).                                              */
  // alert("div_content before = " + div_content);
	div_content = div_content.replace(rem_string, '');
	// alert("div_content after = " + div_content);

  /* Write the new contents of the 'interviewer_fields' division into the    */
  /* HTML on the document page.                                              */
	document.getElementById(div_id).innerHTML = div_content;

  /* Call ChangeAddIntNote to adjust the note regarding adding additional    */
  /* interviewers' details in view of the new number of interviewer details. */
	ChangeAddIntNote(changes_ok_till);	
	
} /* end of RemoveInterviewer function. */

// Function that adds the fields to submit a further Spouse or Visitor's 
// details in the School Fair Registration Form.
function AddVisitor(visitor_num, changes_ok_till) {
  /* Set the variables we are going to use later on.                         */
	var div_id = "visitor_fields";
	var paragraph_id = "add_visitor";
	var new_visitor_num = Number(visitor_num) + 1;

  /* Obtain the full current contents of the 'visitor_fields' division       */
  /* to which we will append the blank form fields which will allow the      */
  /* school to fill in the details of an additional visitor.                 */
	var div_content = document.getElementById(div_id).innerHTML;
	// alert(div_content);

  /* Set the variable containing the HTML that writes the fields             */
  /* into which the school can write an additional interviewer's details.    */
	var extra_vis_html = '<!-- start vis_details_' + visitor_num + ' -->\n';
	extra_vis_html += '<table id="vis_table_' + visitor_num + '" class="boxed-item">\n';
	extra_vis_html += '  <tbody>\n';
	extra_vis_html += '  <tr>\n';
	extra_vis_html += '    <td>\n';
	extra_vis_html += '    <p class="int-sect-heading">Spouse or Visitor:</p>\n';
	extra_vis_html += '          <span id="vis_name_' + visitor_num + '_hd" class="form-vis-row-heading">First Name</span>\n';
	extra_vis_html += '    <input type="text" name="rec_vis_name[]" id="rec_vis_name_' + visitor_num;
	extra_vis_html += '" title="Visitor Name" onchange="ChangeDefaultValue(this.id);" value="" size="40" /><br />\n';
	extra_vis_html += '            <span id="vis_surname_' + visitor_num + '_hd" class="form-vis-row-heading">Surname</span>\n';
	extra_vis_html += '    <input type="text" name="rec_vis_surname[]" id="rec_vis_surname_' + visitor_num;
	extra_vis_html += '" title="Visitor Surname" onchange="ChangeDefaultValue(this.id);" value="" size="40" /><br />\n';
	extra_vis_html += '            <span id="vis_role_' + visitor_num;
	extra_vis_html += '_hd" class="form-vis-row-heading">Role in your school\'s team</span>\n';
	extra_vis_html += '    <input type="text" name="rec_vis_role[]" id="rec_vis_role_' + visitor_num;
	extra_vis_html += '" title="Visitor Role" onchange="ChangeDefaultValue(this.id);" value="Team Member" size="40" /><br />\n';
	extra_vis_html += '            <a href="#" class="float-right" onclick="javascript:RemoveVisitor(';
	extra_vis_html += visitor_num + ', \'' + changes_ok_till + '\'); return false;">Remove This Spouse or Visitor</a>\n';
	extra_vis_html += '    </td>\n';
	extra_vis_html += '  </tr>\n';
	extra_vis_html += '  </tbody>\n';
	extra_vis_html += '</table>\n';
	extra_vis_html += '<!-- end vis_details_' + visitor_num + ' -->';

  /* Append the additional interviewer's blank fields to the existing ones.  */
  var new_div_content = div_content + extra_vis_html;
	// alert(new_div_content);

  /* Set the 'visitor_fields' span contents to the new HTML, which now       */
  /* includes the blank fields into which the school can write an            */
  /* additional interviewer's details.                                       */
  document.getElementById(div_id).innerHTML = new_div_content;

  /* Re-write the 'Add A Spouse or Visitor's Details' link so that it will   */
  /* pass in the right interviewer index number to this function next time.  */
  var new_add_vis_paragraph = '<a href="#" onclick="javascript:AddVisitor(';
	new_add_vis_paragraph += new_visitor_num + ', \'' + changes_ok_till;
	new_add_vis_paragraph += '\'); return false;">Add A Spouse or Visitor\'s Details</a>';
	document.getElementById(paragraph_id).innerHTML = new_add_vis_paragraph;

} /* End of AddVisitor function. */


// Function that removes an Spouse or Visitor's details from the School Fair
// Registration Form.
function RemoveVisitor(rem_vis_num, changes_ok_till) {
	// alert("in RemoveVisitor(" + rem_vis_num + ', ' + changes_ok_till + ")");
	
  /* Set the variables we are going to use later on.                         */
	var div_id = "visitor_fields";
	
  /* Set the variables containing start and end strings of of the section    */
  /* of HTML we need to remove from the 'visitor_fields' division.           */
	var rem_str_start = '<!-- start vis_details_' + rem_vis_num + ' -->';
	var rem_str_end = '<!-- end vis_details_' + rem_vis_num + ' -->';
	// alert("rem_str_start = " + rem_str_start + ";  rem_str_end = " + rem_str_end + ";   rem_str_end.length = " + rem_str_end.length);
	
  /* Obtain the full current contents of the 'visitor_fields' division,      */
  /* from which we will be removing the form fields corresponding to the     */
  /* visitor whose index number has been passed in.                          */
  /* We lower-case this string because the property names in HTML markups    */
  /* may have already been lower-cased by the browser, automatically.        */
	var div_content = document.getElementById(div_id).innerHTML;
	// alert("div_content = " + div_content);
	var lc_div_content = div_content.toLowerCase();
	// alert("lc_div_content = " + lc_div_content);

  /* Search for the position of the start and end of the section             */
  /* corresponding to the visitor whose details we need to remove.           */
  /* To avoid problems due to browsers changing the case of HTML markups     */
  /* we search for a lower-cased string within a lower-case string.          */
	var start_index = lc_div_content.indexOf(rem_str_start.toLowerCase());
	var end_index = lc_div_content.indexOf(rem_str_end.toLowerCase()) + rem_str_end.length;
	// alert("start_index = " + start_index + ";  end_index = " + end_index);
	
	if ((start_index == -1) || (end_index == (rem_str_end.length - 1))) {
		alert("This feature does not work with your browser. \nPlease use a more recent browser version to fill-in this form.");
		return (false);
	}

  /* Obtain the string between the start and end indeces of the section      */
  /* corresponding to the visitor whose details we need to remove            */
	var rem_string = div_content.substring(start_index, end_index);
	// alert("rem_string = " + rem_string);

  /* Replace the details of the visitor we are removing with an empty        */
  /* string (ie: with nothing).                                              */
  // alert("div_content before = " + div_content);
	div_content = div_content.replace(rem_string, '');
	// alert("div_content after = " + div_content);

  /* Write the new contents of the 'visitor_fields' division into the        */
  /* HTML on the document page.                                              */
	document.getElementById(div_id).innerHTML = div_content;

} /* end of RemoveVisitor function. */


// Function that makes sure that as interviewer's details are added to the School Fair
// Registration Form they are written into the "Value" field of the input, lest they
// otherwise be lost when an interviewer is added or removed from the form.
function ChangeDefaultValue(input_id) {
  /* Set the default value for this input field to its current value, which  */
  /* has just been written in by the user.                                   */
	document.getElementById(input_id).defaultValue = document.getElementById(input_id).value;
}


// Function that checks that every field in the School Fair Registration
// Form has been filled in and blocks the submission and pops an alert
// message if any field as been left blank.
function CheckFormIsComplete(caller_form) {
  /* Initialise the string we'll use keep track of the status of the radio   */
  /* inputs.                                                                 */
  var radios_checked = '';
  var radios_unchecked = '';
  
  /* Set the alert message, as we'll use it in more than one place.          */
  var alert_message = 'This form is incomplete.  \nYou must fill in every field in this form before submitting it.  ';

  /* Loop through every field (element) in the form checking that it is not  */
  /* still blank, or - in the case of radios - that at least one radio of    */
  /* each given name has indeed been checked.                                */
  for (ii = 0; ii < caller_form.length; ii++) {
    // alert('checking ' + caller_form.elements[ii].name + ' (= ' + caller_form.elements[ii].value + ') of type = ' + caller_form.elements[ii].type);
		
    /* If the form element is a radio, we need to check that at least one    */
    /* radio with this name has been checked.  However, it may not be the    */
    /* first radio with this name that is checked, so we must track whether  */
    /* (a) we've encountered a radio of this name that has been checked, and */
    /* (b) if not, we need to record whether it is checked or not - and if   */
    /* this instance is checked then we need to remove its name from the     */
    /* unchecked list.                                                       */
    if (caller_form.elements[ii].type.toLowerCase() == 'radio') {
      /* The element is a radio input.                                       */
      if (radios_checked.indexOf(caller_form.elements[ii].name) == -1) {
        /* No radio with this name has yet been checked.  Had a radio with   */
        /* this name already been checked we could have ignored it.          */
        if (!caller_form.elements[ii].checked) {
          if (radios_unchecked.indexOf(caller_form.elements[ii].name) == -1) {
            /* This instance of the radio is not checked and it is the first   */
            /* unchecked instance of a radio with this name, so we add it to   */
            /* the list of unchecked radios.                                   */
            // alert(caller_form.elements[ii].name + ' is NOT checked and not yet in the unchecked string');
            radios_unchecked += caller_form.elements[ii].name + '%%%';
          }
        }
        else {
          /* This instance of the radio has been checked, so we must add it  */
          /* to our list of checked radios.  NOTE only one instance of a     */
          /* radio can be checked, so it can't already be in the list ...in  */
          /* any case, we would have not got this far were it already in the */
          /* list of checked radios.                                         */
          // alert(caller_form.elements[ii].name + ' IS checked');
          radios_checked += caller_form.elements[ii].name + '%%%';
          // alert('radios_unchecked before = ' + radios_unchecked);
          
          /* If a radio with this name appears in our list of unchecked      */
          /* radios this replace will remove it from the list.               */
          radios_unchecked = radios_unchecked.replace(caller_form.elements[ii].name + '%%%', '');
          // alert('radios_unchecked after = ' + radios_unchecked);
        }
      }
    }

    if ((caller_form.elements[ii].value == '') &&
    		(caller_form.elements[ii].type.toLowerCase() != 'hidden')) {
      /* The element has no value set. ie: it's still an empty string, which */
      /* means that the user has not filled in this field in the form yet.   */
      /* We instruct them to fill in the form completely before submitting   */
      /* it and block the submission by returning 'false'.                   */
      /* NOTE Radios all have a pre-set value, so would not cause you to     */
      /* enter this branch.                                                  */
      alert(alert_message + '\n' + caller_form.elements[ii].title + ' field is still blank.  ');
      // alert(caller_form.elements[ii].name + ' is still blank');
      return (false);
    }

    /* If this is an email field, check that the e-mail address entered is   */
    /* correctly formatted.                                                  */
    if (caller_form.elements[ii].name.indexOf('email') != -1) {
      /* Set up a variable containing the regular expression all e-mail      */
      /* addresses must conform with, and others containing a variety of     */
      /* likely typos.                                                       */
      var email_regexp = new RegExp('^[a-zA-Z0-9]+[\\w\\-\\.]*@[a-zA-Z0-9]+[\\w\\-\\.]*\\.[a-zA-Z]{2,4}$');
      var email_nono1 = new RegExp('\\.\\.');
      var email_nono2 = new RegExp('\\.@');
      var email_nono3 = new RegExp('\\_\\.');
      var email_nono4 = new RegExp('\\-\\.');
      var email_nono5 = new RegExp('\\_@');
      var email_nono6 = new RegExp('\\-@');
      var email_nono7 = new RegExp('\\.\\_');
      var email_nono8 = new RegExp('\\_\\_');
      var email_nono9 = new RegExp('\\-\\_');
      var email_nono10 = new RegExp('\\.\\-');
      var email_nono11 = new RegExp('\\_\\-');
      var email_nono12 = new RegExp('\\-\\-');
      
      /* If the e-mail address appears to contain a likely typo, ask the     */
      /* user to check it before proceeding.                                 */
      if ((email_nono3.test(caller_form.elements[ii].value)) ||
          (email_nono4.test(caller_form.elements[ii].value)) ||
          (email_nono5.test(caller_form.elements[ii].value)) ||
          (email_nono6.test(caller_form.elements[ii].value)) ||
          (email_nono7.test(caller_form.elements[ii].value)) ||
          (email_nono8.test(caller_form.elements[ii].value)) ||
          (email_nono9.test(caller_form.elements[ii].value)) ||
          (email_nono10.test(caller_form.elements[ii].value)) ||
          (email_nono11.test(caller_form.elements[ii].value)) ||
          (email_nono12.test(caller_form.elements[ii].value)) ) {
        var email_prompt_text = 'Please check this e-mail address carefully.  ';
        email_prompt_text += 'Correct it if necessary and then hit OK.  ';
        email_prompt_text += '(Or hit Cancel to return to the form without submitting it)';
        var corr_email_val = prompt(email_prompt_text, caller_form.elements[ii].value);

        /* If the user hits 'Cancel' return false to block the submission of */
        /* the form.  If they hit 'OK', reset the value of the field to the  */
        /* post-check e-mail address and allow the submission to go ahead.   */
        if (corr_email_val == null) {
          return (false);
        }
        else {
          caller_form.elements[ii].value = corr_email_val;
          caller_form.elements[ii].defaultValue = corr_email_val;
        }
      }
      /* If the e-mail address submitted does is not correctly formatted     */
      /* warn the user and return false to block the submission of the form  */
      if ((!email_regexp.test(caller_form.elements[ii].value)) ||
          (email_nono1.test(caller_form.elements[ii].value)) ||
          (email_nono2.test(caller_form.elements[ii].value)) ) {
        alert(caller_form.elements[ii].value + ' is not a properly formatted e-mail address.');
        return (false);
      }
    }
  }
  if (radios_unchecked != '') {
    /* Having looked at all the elements in the form, it appears there is at */
    /* least one set of radios that has not been checked.                    */
    /* We instruct the user to fill in the form completely before submitting */
    /* the form and block the submission by returning 'false'.               */
    alert(alert_message + '\nCheck you\'ve answered every Yes/No question.  ');
    // alert('radios_unchecked = ' + radios_unchecked);
    return (false);
  }
  
  /* If we get this far it means that the form was entirely filled in, so we */
  /* can return 'true', thus allowing the form to be sent successfully.      */
	return (true);
} /* End of CheckFormIsComplete function */


// Function that finds out today's date from the user's computer and fills
// in the hidden last_update input value and writes in the date at the
// bottom of the form.
function FillInDate() {
  /* Set the variables containing the IDs of the spans or form elements      */
  /* we'll be writing the date into.                                         */
  var date_span_id = 'todays_date';
  var date_input_id = 'hidden_form_update_id';

  /* Get today's date from the user's computer and use it to set the date    */
  /* variables we are going to use later.                                    */
  var this_moment = new Date();
  var day_of_week = this_moment.getDay();
  var day_of_week_word = day_to_word_array[day_of_week];
  var day_of_month = this_moment.getDate();
  var month = this_moment.getMonth();
  var month_word = month_to_word_array[month];
  var year = this_moment.getFullYear();

  /* Write in today's date into the 'Date' section of the Registration Form. */
  document.getElementById(date_span_id).innerHTML = day_of_week_word + ', ' + day_of_month + ' ' + month_word + ' ' + year;

  /* For the SQL database we need the day and month to be formatted a two    */
  /* digits, and counting up from 1 rather than zero.                        */
  if (day_of_month < 10) {
    var day_of_month = '0' + day_of_month;
  }
  month = Number(month) + 1;
  if (month < 10) {
    var month = '0' + month;
  }
  
  /* Assemble the date stamp that we'll pass into the database.               */
  var current_date_stamp = year + '-' + month + '-' + day_of_month;

  /* Update the date in the hidden Last Update field, so that it will display */
  /* the date from the user's computer.                                       */
  /* Just to be safe, set the hidden element's default value as well as       */
  /* its value.                                                               */
  document.getElementById(date_input_id).defaultValue = current_date_stamp;
  document.getElementById(date_input_id).value = current_date_stamp;

} /* End of FillInDate function */


// Function to fill an address in, in a way that will fool farmers.
function FillIn(who, prfx) {
	// the @ and the .
	var where_num = (8*8);
	var sep_num = (2*(10+13)); 
	// a=97; e=101; i=105; o=111; u=117;
	var l_num = (3*9*4);
	var c_num = (9*11);
	var unit = '1';
	var whence = '';
	var suffix = '';
	
	if ((prfx == 'sk_link_only') || (prfx == 'sk_all')) {
		suffix = '?&#' + c_num + ';&#97;ll';
		var prefix = '<a href=\"s&#' + unit + '07;y&#' + unit + unit + '2;e'; 	
		switch(who) {
			case 'gh':
				whence = 'g&#' + unit + '01;zh&#97;yd&#' + unit + '01;n'; 
				break;
			default:
				whence = '<span class=\"grey\">[S&#' + unit + '07;y&#' + unit + unit + '2;e Address Missing]</span> ';
				prfx = 'none';
		}
	}
	else {
		var prefix = '<a href=\"&#109;a' + 'i&#108;' + '&#116;o'; 
		
		switch(who) {
			case 'hjd':
				whence = 'd&#' + unit + '01;&#' + unit + '01;lm&#97;n' + '&#' + where_num + ';lox' + '&#' + unit + '05;nf&#' + unit + unit + unit + ';&#' + sep_num + ';c&#' + unit + unit + unit + ';&#' + sep_num + ';th'; 
				break;
			case 'gh':
				whence = 'g&#' + unit + '01;zh&#97;yd&#' + unit + '01;n&#' + where_num + ';s&#' + unit + '01;&#97;rch&#97;ss&#' + unit + unit + unit + ';c&#' + unit + '05;&#97;t&#10' + unit + ';s-ib&#' + sep_num + ';&#' + unit + unit + unit + ';rg'; 
				break;
			case 'arh':
				whence = '&#97;rh&#97;yd&#' + unit + '01;n&#' + where_num + ';s&#' + unit + '01;&#97;rch&#97;ss&#' + unit + unit + unit + ';c&#' + unit + '05;&#97;t&#10' + unit + ';s-ib&#' + sep_num + ';&#' + unit + unit + unit + ';rg'; 
				break;
			case 'gm':
				whence = 's&#' + unit + '01;&#97;rchs&#' + c_num + ';h&#' + unit + unit + unit + ';&#' + unit + unit + unit + ';ls&#' + where_num + ';h&#' + unit + unit + unit + ';tm&#97;&#' + unit + '05;l&#' + sep_num + ';&#' + c_num + ';&#' + unit + unit + unit + ';m';
				break;
			case 'mw': 
				whence = 'search&#' + where_num + ';mdw&#' + unit + '05;&#' + l_num + ';&#' + l_num + ';&#' + unit + '05;&#97;ms&#' + sep_num + ';n&#' + unit + '0' + unit + ';t'; 
				break;
			case 'dc': 
				whence = 'dr&#' + sep_num + ';c&#' + unit + unit + unit + ';p&#' + unit + '01;&#' + where_num + ';v&#' + unit + '05;rg&#' + unit + '05;n&#' + sep_num + ';n&#' + unit + '0' + unit + ';t'; 
				break;
			case 'melw': 
				whence = 'S&#' + unit + '0' + unit + ';&#97;r&#' + c_num + ';h&#67;&#' + unit + '0' + unit + ';ntra&#' +  l_num + ';HQ&#' + where_num + ';&#' + c_num + ';s&#' + sep_num + ';&#' + c_num + ';&#' + unit + unit + unit + ';m'; 
				break;
			default:
				whence = '<span class=\"grey\">[Address Missing]</span> ';
				prfx = 'none';
		}
	}
	
	if (prfx != 'none') {
		document.write( prefix + ':' );   
	}
		
	document.write( whence ); 
	
	if ((prfx == 'sk_link_only') || (prfx == 'sk_all')) {
		document.write( suffix );
	}

	if (prfx != 'none') {
		document.write( '\">' );   
		if ((prfx == 'all') || (prfx == 'sk_all')) {
			document.write( whence + '</a>' );   
		}
	}
	return (false);
} /* End of FillIn function. */

// Function that writes a link closure markup, for use with FillIn function with 'link_only' passed in.
function SlashA() {
	document.write('</a>')
	return (false);
} /* End of SlashA function. */

//
function WriteAddressToEmailConfirmNote(email_address) {
//	alert('Entering WriteAddressToEmailConfirmNote');
	document.getElementById('candidate_email').innerHTML = '(' + email_address + ')';
	return (false);
} /* End of WriteAddressToEmailConfirmNote function. */

