function set_set_none (form,sName,sRest) {
	oForm = document.forms[form]; aTails = sRest.split(',');
	for(var iI = 0; iI < aTails.length; iI++) { oSel = oForm[sName + '_display_' + aTails[iI]]; oSel.checked = 0; }
}
function set_set_all (form,sName,sRest) {
	oForm = document.forms[form]; aTails = sRest.split(','); for(var iI = 0; iI < aTails.length; iI++) { oSel = oForm[sName + '_display_' + aTails[iI]]; oSel.checked = 1; }
}
function set_mset_none (form,sName,sRest) {
	//DUNCAN PAYNE: When all target groups are deselected, deselect all options in compSelect.
	if (sName == "target_groups") {
		//Deselect all
		var compSelectBox = document.forms[form]["competency_focus_display"].options;
		for (var i=0; i<compSelectBox.length; i++) {
			compSelectBox[i].selected = '';
		}
	}
	oForm = document.forms[form]; 
	for(var iI = 1; iI <= sRest; iI++) { oSel = oForm[form + '_' + sName + '_display_' + iI]; oSel.checked = 0; }
}
function set_mset_all (form,sName,sRest) {
	oForm = document.forms[form]; 
	for(var iI = 1; iI <= sRest; iI++) { 
		oSel = oForm[form + '_' + sName + '_display_' + iI]; 
		if (oSel.disabled != 1) { oSel.checked = 1; }
	}
}
// Selects the options for a multi box on load
function set_multi_display (form,sName) {
	oForm = document.forms[form]; oSel = oForm[sName + '_display']; oValue = oForm[sName]; sSearch = '::' + oValue.value + '::';
	for(var iI = 0; iI < oSel.options.length; iI++)  oSel.options[iI].selected = (sSearch.indexOf('::' + oSel.options[iI].value + '::') != -1);
}

// Selects the options for a multi-group (If done on-load, must be slightly delayed in order to find whole form).
function set_multi_group_display (form,sName,tName) {
	
	oForm = document.forms[form];
	oSel = oForm[sName + '_display'];
	oValue = oForm[sName]; 
	if (document.forms[form][tName])
	  tValues = oForm[tName].value.split('::');
	else 
	  var tValues = {length:1};
	oValues = oForm[sName].value.split('::');
	var sSearch = '';
	for (var a = 0; a < tValues.length; a++){
	 if (tValues[a]!=''){
    	for (var b = 0; b < oValues.length; b++){
    	 if (oValues[b]!=''){
				 sSearch+='::'+tValues[a]+"|"+oValues[b];
    	 }
    	}
	 }
	}
	sSearch += '::';
	for(var iI = 0; iI < oSel.options.length; iI++)  oSel.options[iI].selected = (sSearch.indexOf('::' + oSel.options[iI].value + '::') != -1);
}

// Sorts and removes duplicates in an array
function array_unique(theArray){
  	theArray.sort();
		if (theArray.length>1) {
  	  var i=0;
    	do {
    	  if (theArray[i]==theArray[i+1]){
  			  theArray.splice(i+1,1);
  			} else {
  				i++;
  			}
    	  
    	} while(i+1<theArray.length);
  	}
  return theArray;
}

//Sets hidden field to appropriate values when multi's clicked on
function set_multi_value (form,sName,tName) {

	oForm = document.forms[form]; oSel = oForm[sName + '_display']; oValue = oForm[sName]; oValue.value = '';
	
	//DUNCAN PAYNE: Limiting numbers of competencies selected
	if (sName == "competency_focus") {
		checkSelect(form, oSel);
		validateCompAspects(form,sName);
	}
	
	if (tName) { oArray = new Array(); tArray = new Array(); tValue = oForm[tName]; tValue.value = ''; }
	
	for(var iI = 0; iI < oSel.options.length; iI++) if (oSel.options[iI].selected) { 
	    if (tName) {
			  oArray.push(oSel.options[iI].value.split("|")[1]);
				tArray.push(oSel.options[iI].value.split("|")[0]);
			} else {
			  oValue.value += '::' + oSel.options[iI].value;
			}
	}
	if (tName) { 
  	tArray = array_unique(tArray);
		oArray = array_unique(oArray);
  	tValue.value = "::"+tArray.join("::")+"::"; 
		oValue.value = oArray.join("::"); 
	} else {
	  oValue.value = oValue.value.substr(2);
	}
}

// Unselects an entire multi box
function set_multi_none (form,sName,tName) {
	oForm = document.forms[form]; oSel = oForm[sName + '_display']; oValue = oForm[sName];
	if (tName) { tValue = oForm[tName]; }
	for(var iI = 0; iI < oSel.options.length; iI++) {
	  oSel.options[iI].selected = false;
	}
	if (tName) { 
  	tValue.value = '';
  	oValue.value = '';
	}
}

// Selects an entire multi box
function set_multi_all (form,sName,tName) {
	oForm = document.forms[form]; oSel = oForm[sName + '_display']; oValue = oForm[sName];
	if (tName) { tValue = oForm[tName]; }
	for(var iI = 0; iI < oSel.options.length; iI++) {
	  oSel.options[iI].selected = true;
	}

	set_multi_value (form,sName,tName);
}

function set_date_display (form,sName) {
	oForm = document.forms[form]; oSelDay = oForm[sName + '_day']; oSelMonth = oForm[sName + '_month']; oSelYear = oForm[sName + '_year']; oValue = oForm[sName];
	if (oValue.value.indexOf('-') == -1) return;
	iDay = oValue.value.split('-')[2]; if (iDay.substr(0,1) == '0') iDay = iDay.substr(1,1); iDay = parseInt(iDay);
	iMonth = oValue.value.split('-')[1]; if (iMonth.substr(0,1) == '0') iMonth = iMonth.substr(1,1); iMonth = parseInt(iMonth);
	iYear = parseInt(oValue.value.split('-')[0]);
	oSelDay.options[iDay].selected = true; oSelMonth.options[iMonth].selected = true;
	for (iI = 0; iI < oSelYear.options.length; iI++) if (parseInt(oSelYear.options[iI].value) == iYear) oSelYear.options[iI].selected = true;
}
function set_date_value (form,sName) {
	oForm = document.forms[form]; oSelDay = oForm[sName + '_day']; oSelMonth = oForm[sName + '_month']; oSelYear = oForm[sName + '_year']; oValue = oForm[sName];
	oValue.value = oSelYear.options[oSelYear.selectedIndex].value + '-' + oSelMonth.options[oSelMonth.selectedIndex].value + '-' + oSelDay.options[oSelDay.selectedIndex].value;
	if (oValue.value.length < 10) oValue.value = '';
}
function populate_date (form,sName, iNumYears, iStartYear) {
	aMonths = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	oForm = document.forms[form]; oSelDay = oForm[sName + '_day']; oSelMonth = oForm[sName + '_month']; oSelYear = oForm[sName + '_year'];
	for (iI = 1; iI <= 31; iI++) { if (iI < 10) sVal = '0' + iI; else sVal = iI; oSelDay.options[iI] = new Option(sVal, sVal); }
	for (iI = 1; iI <= 12; iI++) { if (iI < 10) sVal = '0' + iI; else sVal = iI; oSelMonth.options[iI] = new Option(aMonths[iI - 1], sVal);	}
	for (iI = 0; iI < iNumYears; iI++) { oSelYear.options[iI + 1] = new Option(iStartYear + iI, iStartYear + iI); }
}
function show_help (e) { helpwin = popup(e.href, 'helpwin', 300, 400, 'rc', 'tc', 0, 0, 1); helpwin.focus(); }
function popup (filename, title, width, height, x, y,scrollbars,statusbar,resize,toolbar) {
	if(x=='c') x = Math.round((screen.width / 2) - (width / 2)); if(y=='c') y = Math.round((screen.height / 2) - (height / 2));
	if(x=='lc') x = Math.round((screen.width / 3) - (width / 2)); if(y=='tc') y = Math.round((screen.height / 3) - (height / 2));
	if(x=='rc') x = Math.round(((screen.width / 3) * 2) - (width / 2)); if(y=='bc') y = Math.round(((screen.height / 3) * 2) - (height / 2));
	if(scrollbars == null) scrollbars=1; if(statusbar == null) statusbar=0; if(resize == null) resize=0; if(toolbar == null) toolbar=0;
	NewWindow = window.open(filename, title, "status="+statusbar+",scrollbars="+scrollbars+",resizable="+resize+",toolbar="+toolbar+",menubar="+toolbar+",width="+width+",height="+height+",left="+x+",top="+y);
	if(!NewWindow.opener && navigator.appVersion.indexOf("MSIE 5")==-1) { NewWindow.opener = self; } return NewWindow;
}
function init_form (form) {	if (!form) form = 'form'; for (var i=0; i<document.forms[form].length; i++) if(document.forms[form][i].type!='hidden') {document.forms[form][i].focus();break;}init_buttons(); }


//DUNCAN PAYNE: Validation of target areas and competency selections
function validateComps(form) {
	
	//Look for checkboxes on page that start with string and end with i
	for (var j=1; true; j++) {
		var targetName = document.getElementById("form_target_groups_display_"+j);
		
		if (targetName) {
			
			var compSelectBox = document.forms[form]["competency_focus_display"].options;
			
			var targetClicked = targetName.value;
			
			var OptStart = "Optometrists";
			var D_OStart = "Dispensing Opticians";
			var clStart = "Contact Lens Specialists";
			var tASStart = "Therapeutics Specialists - Additional Supply";
			var tSPStart = "Therapeutics Specialists - Supplementary Prescribing";
			
			for (var i=0; i<compSelectBox.length; i++) {
				if ((startsWith(compSelectBox[i].value,OptStart)) && (targetName.value==OptStart)) {
					if (targetName.checked) {
// 						Enable select items
// 						compSelectBox[i].disabled="";
					}else {
// 						Disable select items
// 						compSelectBox[i].disabled="disabled";
						compSelectBox[i].selected='';
					}
				}else if ((startsWith(compSelectBox[i].value,D_OStart)) && (targetName.value==D_OStart)) {
					if (targetName.checked) {
// 						Enable select items
// 						compSelectBox[i].disabled="";
					}else {
// 						Disable select items
// 						compSelectBox[i].disabled="disabled";
						compSelectBox[i].selected='';
					}
				}else if ((startsWith(compSelectBox[i].value,clStart)) && (targetName.value ==clStart)) {
					if (targetName.checked) {
// 						Enable select items
// 						compSelectBox[i].disabled="";
					}else {
// 						Disable select items
// 						compSelectBox[i].disabled="disabled";
						compSelectBox[i].selected='';
					}
				}else if ((startsWith(compSelectBox[i].value,tASStart)) && (targetName.value ==tASStart)) {
					if (targetName.checked) {
// 						Enable select items
// 						compSelectBox[i].disabled="";
					}else {
// 						Disable select items
// 						compSelectBox[i].disabled="disabled";
						compSelectBox[i].selected='';
					}
				}else if ((startsWith(compSelectBox[i].value,tSPStart)) && (targetName.value ==tSPStart)) {
					if (targetName.checked) {
// 						Enable select items
// 						compSelectBox[i].disabled="";
					}else {
// 						Disable select items
// 						compSelectBox[i].disabled="disabled";
						compSelectBox[i].selected='';
					}
				}
			}
		}else {
			break;
		}
	}
}


function checkSelect(form, oSel) {
	var opts = oSel.options;
	
	var OptSelectCount = 0;
	var D_OSelectCount = 0;
	var clSelectCount = 0;
	var tASSelectCount = 0;
	var tSPSelectCount = 0;
	
	var maxCompSelected = 3;
	
	var OptStart = "Optometrists";
	var D_OStart = "Dispensing Opticians";
	var clStart = "Contact Lens Specialists";
	var tASStart = "Therapeutics Specialists - Additional Supply";
	var tSPStart = "Therapeutics Specialists - Supplementary Prescribing";
	
	for (var i=0; i<opts.length; i++) {
		//Values are made up of the following: "optgroup|option"
		//BAD PRACTICE: fixed strings (target_groups_display_#) are used, but ideally this should not happen
		if ((startsWith(opts[i].value,OptStart)) && (opts[i].selected)) {
			if (selectAllowed(form,oSel,"target_groups_display_1")) {
				OptSelectCount++;
				
				if (OptSelectCount > maxCompSelected) {
					alert("You cannot select more than "+ maxCompSelected +" competencies per target group.");
					opts[i].selected = '';
					OptSelectCount--;
				}
			}else {
				opts[i].selected = '';
				alert("You cannot select a competency for a target area which has not been selected");
			}
		}else if ((startsWith(opts[i].value,D_OStart)) && (opts[i].selected)) {
			if (selectAllowed(form,oSel,"target_groups_display_2")) {
				D_OSelectCount++;
				
				if (D_OSelectCount > maxCompSelected) {
					alert("You cannot select more than "+ maxCompSelected +" competencies per target group.");
					opts[i].selected = '';
					D_OSelectCount--;
				}
			}else {
				opts[i].selected = '';
				alert("You cannot select a competency for a target area which has not been selected");
			}
		}else if ((startsWith(opts[i].value,clStart)) && (opts[i].selected)) {
			if (selectAllowed(form,oSel,"target_groups_display_3")) {
				clSelectCount++;
				
				if (clSelectCount > maxCompSelected) {
					alert("You cannot select more than "+ maxCompSelected +" competencies per target group.");
					opts[i].selected = '';
					clSelectCount--;
				}
			}else {
				opts[i].selected = '';
				alert("You cannot select a competency for a target area which has not been selected");
			}
		}else if ((startsWith(opts[i].value,tASStart)) && (opts[i].selected)) {
			if (selectAllowed(form,oSel,"target_groups_display_4")) {
				tASSelectCount++;
				
				if (tASSelectCount > maxCompSelected) {
					alert("You cannot select more than "+ maxCompSelected +" competencies per target group.");
					opts[i].selected = '';
					tASSelectCount--;
				}
			}else {
				opts[i].selected = '';
				alert("You cannot select a competency for a target area which has not been selected");
			}
		}else if ((startsWith(opts[i].value,tSPStart)) && (opts[i].selected)) {
			if (selectAllowed(form,oSel,"target_groups_display_5")) {
				tSPSelectCount++;
				
				if (tSPSelectCount > maxCompSelected) {
					alert("You cannot select more than "+ maxCompSelected +" competencies per target group.");
					opts[i].selected = '';
					tSPSelectCount--;
				}
			}else {
				opts[i].selected = '';
				alert("You cannot select a competency for a target area which has not been selected");
			}
		}
	}
}


function selectAllowed(form,selectName,targetName) {
	//if(typeof(document.forms[form][targetName])!='undefined') {
	if(document.forms[form][targetName] != null) {
		if (document.forms[form][targetName].checked) {
			return true;
		}else {
			return false;
		}
	}
	else {
		return true;
	}
}


function pageLoadedCalcSelected(form) {
				document.getElementById("form_submit").style.background="#666666";
				document.getElementById("form_submit").disabled="disabled";
				//return true;
	var err_on_page = calcSelected(form,'optom_comm_skills');
	if (!err_on_page) err_on_page = calcSelected(form,'optom_prof_conduct');
	if (!err_on_page) err_on_page = calcSelected(form,'optom_visual_funct');
	if (!err_on_page) err_on_page = calcSelected(form,'optom_optical_appliances');
	if (!err_on_page) err_on_page = calcSelected(form,'optom_ocular_examination');
	if (!err_on_page) err_on_page = calcSelected(form,'optom_ocular_abnormalities');
	if (!err_on_page) err_on_page = calcSelected(form,'optom_contact_lenses');
	if (!err_on_page) err_on_page = calcSelected(form,'optom_binocular_vision');

//DISPENSING OPTICIANS
	if (!err_on_page) err_on_page = calcSelected(form,'do_comm_skills');
	if (!err_on_page) err_on_page = calcSelected(form,'do_prof_conduct');
	if (!err_on_page) err_on_page = calcSelected(form,'do_refractive_management');
	if (!err_on_page) err_on_page = calcSelected(form,'do_optical_appliances');
	if (!err_on_page) err_on_page = calcSelected(form,'do_contact_lenses');
	if (!err_on_page) err_on_page = calcSelected(form,'do_low_vision');
	if (!err_on_page) err_on_page = calcSelected(form,'do_ocular_exam');
	if (!err_on_page) err_on_page = calcSelected(form,'do_ocular_abnormalities');

//CONTACT LENS SPECIALIST OR THERAPEUTIC
	if (!err_on_page) err_on_page = calcSelected(form,'contact_lens_practice');

//THERAPEUTICS - ADDITIONAL SUPPLY
	if (!err_on_page) err_on_page = calcSelected(form,'as_clinical_and_pharm');
	if (!err_on_page) err_on_page = calcSelected(form,'as_establish_options');
	if (!err_on_page) err_on_page = calcSelected(form,'as_communicating_with_patients');
	if (!err_on_page) err_on_page = calcSelected(form,'as_prescribing_safely');
	if (!err_on_page) err_on_page = calcSelected(form,'as_prescribing_professionally');
	if (!err_on_page) err_on_page = calcSelected(form,'as_improving_prescribing_practice');
	if (!err_on_page) err_on_page = calcSelected(form,'as_information_in_context');
	if (!err_on_page) err_on_page = calcSelected(form,'as_nhs_in_context');
	if (!err_on_page) err_on_page = calcSelected(form,'as_team_and_indiv_context');

//THERAPEUTICS - SUPPLEMENTARY PRESCRIBING
	if (!err_on_page) err_on_page = calcSelected(form,'sp_establishing_options');
	if (!err_on_page) err_on_page = calcSelected(form,'sp_prescribing_safely');
	if (!err_on_page) err_on_page = calcSelected(form,'sp_prescribing_professionally');
	if (!err_on_page) err_on_page = calcSelected(form,'sp_nhs_in_context');
	if (!err_on_page) err_on_page = calcSelected(form,'sp_team_and_indiv_context');
	//document.write(err_on_page);
}

//DUNCAN PAYNE - Implementation of the check on the number of selections made for sub comps
function calcSelected(form,checkBoxGroup) {
	var minSelections = 1;
	var maxSelections = 5;
	var currentSelections = 0;
	var boxNumbers=0;
	var returnValue = false;
	
	var checkBoxGroupName = "form_"+checkBoxGroup+"_display_";
	
	//Loop on all checkBoxGroup titles. Break if no more found.
	for (var j=1; true; j++) {
		var checkBox = document.getElementById(checkBoxGroupName+j);
		if (checkBox) {
			boxNumbers++;
		}else {
			break;
		}
	}
	
	for (var j=1; true; j++) {
		var checkBox = document.getElementById(checkBoxGroupName+j);
		
		if (checkBox) {
			//Check box exists (and checked). Test to see if checked. If so, add to running total.
			if (checkBox.checked) {
				currentSelections++;
			}
			
			if ((boxNumbers<minSelections) || ((currentSelections>=minSelections) && (currentSelections<=maxSelections))) {
				//Allowed to proceed
				document.getElementById("form_submit").disabled="";
				document.getElementById("form_submit").style.background="#0061ab";
				
				returnValue = false;
			}else {
				//Stopped from moving forward
				document.getElementById("form_submit").style.background="#666666";
				document.getElementById("form_submit").disabled="disabled";
				if (currentSelections>maxSelections) {
					alert ("You have made too many selections. You are only allowed to\nselect "+maxSelections+" selections per competency. Please choose fewer\ncomponents.");
					break;
				}
				
				returnValue = true;
			}
		}else {
			//Check box by that title does not exist, so break
			return returnValue;
		}
	}
}


//DUNCAN PAYNE - Implementation of a check on whether competencies should be enabled
function validateCompAspects(form,competency) {
	var compSelectBox = document.forms[form][competency+"_display"].options;
	
	for (var i=0; i<compSelectBox.length; i++) {
		var selectValue = compSelectBox[i].value;
		
		if (compSelectBox[i].selected) {
			//check to see which it is, and enable checkboxes for aspects
			if (selectValue=="Optometrists|Communication Skills for Optometrists") {
				enableCheckboxGroup('optom_comm_skills',false);
			}else if (selectValue=="Optometrists|Professional Conduct for Optometrists") {
				enableCheckboxGroup('optom_prof_conduct',false);
			}else if (selectValue=="Optometrists|Visual Function for Optometrists") {
				enableCheckboxGroup('optom_visual_funct',false);
			}else if (selectValue=="Optometrists|Optical Appliances for Optometrists") {
				enableCheckboxGroup('optom_optical_appliances',false);
			}else if (selectValue=="Optometrists|Ocular Examination for Optometrists") {
				enableCheckboxGroup('optom_ocular_examination',false);
			}else if (selectValue=="Optometrists|Ocular Abnormalities for Optometrists") {
				enableCheckboxGroup('optom_ocular_abnormalities',false);
			}else if (selectValue=="Optometrists|Contact Lenses for Optometrists") {
				enableCheckboxGroup('optom_contact_lenses',false);
			}else if (selectValue=="Optometrists|Binocular Vision for Optometrists") {
				enableCheckboxGroup('optom_binocular_vision',false);
			}
			
			//DISPENSING OPTICIANS
			else if (selectValue=="Dispensing Opticians|Communication Skills for Dispensing Opticians") {
				enableCheckboxGroup('do_comm_skills',false);
			}else if (selectValue=="Dispensing Opticians|Professional Conduct for Dispensing Opticians") {
				enableCheckboxGroup('do_prof_conduct',false);
			}else if (selectValue=="Dispensing Opticians|Refractive Management for Dispensing Opticians") {
				enableCheckboxGroup('do_refractive_management',false);
			}else if (selectValue=="Dispensing Opticians|Optical Appliances for Dispensing Opticians") {
				enableCheckboxGroup('do_optical_appliances',false);
			}else if (selectValue=="Dispensing Opticians|Contact Lenses for Dispensing Opticians") {
				enableCheckboxGroup('do_contact_lenses',false);
			}else if (selectValue=="Dispensing Opticians|Low Vision for Dispensing Opticians") {
				enableCheckboxGroup('do_low_vision',false);
			}else if (selectValue=="Dispensing Opticians|Optical Examination and Technique") {
				enableCheckboxGroup('do_ocular_exam',false);
			}else if (selectValue=="Dispensing Opticians|Ocular Abnormalities for Dispensing Opticians") {
				enableCheckboxGroup('do_ocular_abnormalities',false);
			}
			
			//CONTACT LENS
			else if (selectValue=="Contact Lens Specialists|Contact Lens Practice") {
				enableCheckboxGroup('contact_lens_practice',false);
			}
			
			//Therapeutics Specialists
			else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Clinical and Pharmaceutical Knowledge") {
				enableCheckboxGroup('as_clinical_and_pharm',false);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Establishing Options") {
				enableCheckboxGroup('as_establish_options',false);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Communicating with Patients") {
				enableCheckboxGroup('as_communicating_with_patients',false);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Prescribing Safely") {
				enableCheckboxGroup('as_prescribing_safely',false);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Prescribing Professionally") {
				enableCheckboxGroup('as_prescribing_professionally',false);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Improving Prescribing Practice") {
				enableCheckboxGroup('as_improving_prescribing_practice',false);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Information in Context") {
				enableCheckboxGroup('as_information_in_context',false);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - The NHS in Context") {
				enableCheckboxGroup('as_nhs_in_context',false);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - The Team and Individual Context") {
				enableCheckboxGroup('as_team_and_indiv_context',false);
			}else if (selectValue=="Therapeutics Specialists - Supplementary Prescribing|SP - Establishing Options") {
				enableCheckboxGroup('sp_establishing_options',false);
			}else if (selectValue=="Therapeutics Specialists - Supplementary Prescribing|SP - Prescribing Safely") {
				enableCheckboxGroup('sp_prescribing_safely',false);
			}else if (selectValue=="Therapeutics Specialists - Supplementary Prescribing|SP - Prescribing Professionally") {
				enableCheckboxGroup('sp_prescribing_professionally',false);
			}else if (selectValue=="Therapeutics Specialists - Supplementary Prescribing|SP - The NHS in Context") {
				enableCheckboxGroup('sp_nhs_in_context',false);
			}else if (selectValue=="Therapeutics Specialists - Supplementary Prescribing|SP - The Team and Individual Context") {
				enableCheckboxGroup('sp_team_and_indiv_context',false);
			}
		}else {
			//disable ckeck boxes for relevant aspect
			if (selectValue=="Optometrists|Communication Skills for Optometrists") {
				enableCheckboxGroup('optom_comm_skills',true);
			}else if (selectValue=="Optometrists|Professional Conduct for Optometrists") {
				enableCheckboxGroup('optom_prof_conduct',true);
			}else if (selectValue=="Optometrists|Visual Function for Optometrists") {
				enableCheckboxGroup('optom_visual_funct',true);
			}else if (selectValue=="Optometrists|Optical Appliances for Optometrists") {
				enableCheckboxGroup('optom_optical_appliances',true);
			}else if (selectValue=="Optometrists|Ocular Examination for Optometrists") {
				enableCheckboxGroup('optom_ocular_examination',true);
			}else if (selectValue=="Optometrists|Ocular Abnormalities for Optometrists") {
				enableCheckboxGroup('optom_ocular_abnormalities',true);
			}else if (selectValue=="Optometrists|Contact Lenses for Optometrists") {
				enableCheckboxGroup('optom_contact_lenses',true);
			}else if (selectValue=="Optometrists|Binocular Vision for Optometrists") {
				enableCheckboxGroup('optom_binocular_vision',true);
			}
			
			//DISPENSING OPTICIANS
			else if (selectValue=="Dispensing Opticians|Communication Skills for Dispensing Opticians") {
				enableCheckboxGroup('do_comm_skills',true);
			}else if (selectValue=="Dispensing Opticians|Professional Conduct for Dispensing Opticians") {
				enableCheckboxGroup('do_prof_conduct',true);
			}else if (selectValue=="Dispensing Opticians|Refractive Management for Dispensing Opticians") {
				enableCheckboxGroup('do_refractive_management',true);
			}else if (selectValue=="Dispensing Opticians|Optical Appliances for Dispensing Opticians") {
				enableCheckboxGroup('do_optical_appliances',true);
			}else if (selectValue=="Dispensing Opticians|Contact Lenses for Dispensing Opticians") {
				enableCheckboxGroup('do_contact_lenses',true);
			}else if (selectValue=="Dispensing Opticians|Low Vision for Dispensing Opticians") {
				enableCheckboxGroup('do_low_vision',true);
			}else if (selectValue=="Dispensing Opticians|Optical Examination and Technique") {
				enableCheckboxGroup('do_ocular_exam',true);
			}else if (selectValue=="Dispensing Opticians|Ocular Abnormalities for Dispensing Opticians") {
				enableCheckboxGroup('do_ocular_abnormalities',true);
			}
			
			//CONTACT LENS
			else if (selectValue=="Contact Lens Specialists|Contact Lens Practice") {
				enableCheckboxGroup('contact_lens_practice',true);
			}
			
			//Therapeutics Specialists
			else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Clinical and Pharmaceutical Knowledge") {
				enableCheckboxGroup('as_clinical_and_pharm',true);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Establishing Options") {
				enableCheckboxGroup('as_establish_options',true);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Communicating with Patients") {
				enableCheckboxGroup('as_communicating_with_patients',true);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Prescribing Safely") {
				enableCheckboxGroup('as_prescribing_safely',true);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Prescribing Professionally") {
				enableCheckboxGroup('as_prescribing_professionally',true);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Improving Prescribing Practice") {
				enableCheckboxGroup('as_improving_prescribing_practice',true);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - Information in Context") {
				enableCheckboxGroup('as_information_in_context',true);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - The NHS in Context") {
				enableCheckboxGroup('as_nhs_in_context',true);
			}else if (selectValue=="Therapeutics Specialists - Additional Supply|AS - The Team and Individual Context") {
				enableCheckboxGroup('as_team_and_indiv_context',true);
			}else if (selectValue=="Therapeutics Specialists - Supplementary Prescribing|SP - Establishing Options") {
				enableCheckboxGroup('sp_establishing_options',true);
			}else if (selectValue=="Therapeutics Specialists - Supplementary Prescribing|SP - Prescribing Safely") {
				enableCheckboxGroup('sp_prescribing_safely',true);
			}else if (selectValue=="Therapeutics Specialists - Supplementary Prescribing|SP - Prescribing Professionally") {
				enableCheckboxGroup('sp_prescribing_professionally',true);
			}else if (selectValue=="Therapeutics Specialists - Supplementary Prescribing|SP - The NHS in Context") {
				enableCheckboxGroup('sp_nhs_in_context',true);
			}else if (selectValue=="Therapeutics Specialists - Supplementary Prescribing|SP - The Team and Individual Context") {
				enableCheckboxGroup('sp_team_and_indiv_context',true);
			}
		}
	}
}


//DUNCAN PAYNE - Choose whether to enable a check box group
function enableCheckboxGroup(groupName,disable) {
// 	var styleDisplay = document.getElementById();
	
	if(document.getElementById && (styleDisplay = document.getElementById(groupName+'_row')) && styleDisplay.style) {
		if (!disable) {
			styleDisplay.style.display="";
		}else {
			styleDisplay.style.display="none";
		}
	}
	
	for (var i=1; true; i++) {
		var checkBoxGroup = document.getElementById("form_"+groupName+"_display_"+i);
		
		if (checkBoxGroup) {
			if (disable) {
				checkBoxGroup.disabled="disabled";
			}else {
				checkBoxGroup.disabled="";
			}
		}else {
			//No more check boxes in this group
			break;
		}
	}
}


//DUNCAN PAYNE - Implementation of a startsWith function
function startsWith(haystack, needle, from, to) {
	return haystack.startswith(needle,from,to);
}

String.prototype.startswith = function(s, from , to) {
	from = from || 0;
	to = to || 9999999; // Number.MAX_VALUE seems to bug with string.slice()
	s = typeof s == "string" ? [s] : s;
	
	for ( var n=0; n<s.length; n++) {
		e = s[n];
		if (e=='' || this.slice(from, to).indexOf(e) == 0) return true;
	}
	return false;
}

function showSections(fName,sName,tName) {
	if(document.getElementById) {

		var form = document.forms[fName];
		var num_selected = form[tName].value;

		if(tName == "num_posters") {
			var section2 = document.getElementById(sName+'2');
			if(num_selected == "8_posters_16_MCQs") {
				section2.style.display = "";
			}
			else {
				section2.style.display = "none";
			}
		}
		else {
			var section2 = document.getElementById(sName+'2');
			var section3 = document.getElementById(sName+'3');

			if((num_selected == 2)||(num_selected=="8_posters_16_MCQs")) {
				section2.style.display = "";
				section3.style.display = "none";
			}
			else if(num_selected == 3) {
				section2.style.display = "";
				section3.style.display = "";
			}
			else {
				section2.style.display = "none";
				section3.style.display = "none";
			}
		}
	}
}
		
