function showSittings(idPrefix, number, total) {
	for(var i = 0; i < total; i++) {
		if( i < number ) {
			document.getElementById( idPrefix + (i+1) ).style.display = "block";
//				document.getElementById("exam" (i+1) ).disabled = true;
		} else {
			document.getElementById( idPrefix + (i+1) ).style.display = "none";
		}
	}
}

function showFormLoadProgress(targetDivId, srcDivId) {
	document.getElementById(targetDivId).innerHTML = document.getElementById(srcDivId).innerHTML;
}

function getExamForm(formType) {
	var poststr = "nextFormType=" + encodeURI(formType);
	showFormLoadProgress('examArea', 'examloader');
	makePOSTRequest('POST', 'getforms.php', 'examArea' , poststr);
}
	
	/* 
		Code for setting up section for examination grades
	*/
	var combos = [];
	var elecSubs = [];
	var grades = ['Grade','A','B','C','D','E','F'];
	var chosenGrades = [];
	var totalSections = 4;
	
  	function initComboBoxesFor(examType, section) {
		// temporary array for holding values from grades combo box
		var tmpArray;
	
		for(var i = 0; i < combos[section].length; i++) {
			if( examType == "SSSCE" ) {
				tmpArray = document.getElementById('tmpSSSCE').options;
			} else if( examType == "WASSCE" ) {
				tmpArray = document.getElementById('tmpWASSSCE').options;
			} else {
				tmpArray = ["Grade Options Not Available"];
			}

			document.getElementById( combos[section][i] ).options.length = 0;
			document.getElementById( combos[section][i] ).options[0] =  new Option("Grade", "-1", false, false);
			
			for(var j = 1; j <= tmpArray.length; j++) {
				var tmpOption = tmpArray[j-1];
				document.getElementById( combos[section][i] ).options[j] = new Option( tmpOption.text, tmpOption.value, false, false);
			}
			
			
			// provide necessary visual information to end user
			if( tmpArray.length == 1 ) {
				document.getElementById( combos[section][i] ).disabled = true;
				document.getElementById( combos[section][i] ).onchange = null;
				document.getElementById( "instruct" + section ).style.visibility = "visible";
			} else {
				document.getElementById( combos[section][i] ).disabled = false;
				document.getElementById( combos[section][i] ).onchange = function() {
					displayCalculatedGrades(section);
				}
				
				document.getElementById( "instruct" + section ).style.visibility = "hidden";
			}
		}
		
		// reset the display boxes for the best six and total grades
		displayCalculatedGrades(section);
		
		// obtain and update the the no of sittings based on enabled sections
		document.getElementById("NO_SIT").value = getNoOfSittings();
		
		// setup elective courses combo boxes
		for(var i = 0; i < elecSubs[section].length; i++) {
			var comboBox = document.getElementById( elecSubs[section][i] );
			
			if( tmpArray.length == 1 ) {
				comboBox.disabled = true;
				comboBox.onchange = null;
			} else {
				comboBox.disabled = false;
				comboBox.setAttribute("section", section);
				comboBox.setAttribute("index", i );
				
				var gradeBoxId = "EG" + (section > 1 ? section : '') + "" + ( i + 1);
				document.getElementById(gradeBoxId).disabled = true;

				if( comboBox.onchange == null ) {
					comboBox.onchange = function() {
						checkIfUniqueCourseIsChosen( this.getAttribute("section"), this.getAttribute("index") );
						enableGradeBox( this.getAttribute("section"), this.getAttribute("index") );
					}
				}

                // reset the grade boxes if reloading data from database
                enableGradeBox(section, i);
			}
		}
		
		// enable/disable the exam index fields and date of sitting combos
		var examNo = document.getElementById("EXAMNO" + section);
		var mdat = document.getElementById("MDAT" + section);
		var yearBox = document.getElementById("YEAR" + section);
		if( tmpArray.length == 1 ) {
			examNo.value = "";
			mdat.value = mdat.options[0].value;
			yearBox.value = yearBox.options[0].value;
		} else {
			
		}
	}
	
	function enableGradeBox(section, index) {
		for(var j = 0; j < combos[section].length; j++) {
			var comboBox = document.getElementById( elecSubs[section][index] );
			var testValue = "EG" + (section > 1 ? section : '') + "" + ( parseInt(index) + 1);
			//alert("Combo: " + combos[section][j] + " Test: " + testValue + " Combo Value: " + comboBox.value );
			
			if( combos[section][j].indexOf(testValue) != -1 ) {

				if( comboBox.options[ comboBox.selectedIndex ].value != "" ) {
					document.getElementById( combos[section][j] ).disabled = false;	
					break;
				} else {
					document.getElementById( combos[section][j] ).disabled = true;
					document.getElementById( combos[section][j] ).value = document.getElementById( combos[section][j] ).options[0].value;
					break;
				}
			}
		}
	}
	
	function checkIfUniqueCourseIsChosen(section,index) {
		var comboBox = document.getElementById( elecSubs[section][index] );
		for( var j = 0; j < elecSubs[section].length; j++ ) {
			var comparator = document.getElementById( elecSubs[section][j] );
			
			if( j != index && comparator.value == comboBox.value && comparator.value != "" ) {
				alert("You Cannot Choose " + comboBox.options[ comboBox.selectedIndex ].text + " twice. Please Correct Selection");	
				comboBox.value = comboBox.options[0].value;
				break;
			}
		}
	}
	
	function validateSuppliedResults() {
        if( getNoOfSittings() == 0 ) {
            alert("Please Supply At Least 1 Set Of Examination Results");
            return false;
        }

        var errorCount = 0;
        var errorList = "";
        
		for( var i = 1; i <= getNoOfSittings(); i++ ) {
			var examType = document.getElementById("ExamType" + i);
			
			if( examType.value != "" ) {
                var field = document.getElementById("EXAMNO" + i);

                if( field.value == null || field.value == "" || field.value.length == 0  ) {
                    errorCount++;
                    errorList = errorList + " - Please Specify The Examination Index Number For Sitting " + i + "\n";
                }

                field = document.getElementById("MDAT" + i);

                if( field.value != null && (field.value != "6" && field.value != "12") ) {
                    errorCount++;
                    errorList = errorList + " - Please Specify The Date Of Examination Sitting For Sitting " + i + "\n";
                }
				
				field = document.getElementById("YEAR" + i);
				
				if( field.value == null && field.value == "" || field.value.length == 0 ) {
					errorCount++;
					errorList = errorList + " - Please Specify The Year Of Exam Sitting for Sitting " + i + "\n\n";
				}
			}
		}

        if( errorCount > 0 ) {
            var error = "Please Correct The Error(s) Listed Below\n\n" + errorList;
            alert(error);
            return false;
        }

        return true;
	}
	
	function initComboArrays() {
		
		/* create temporary array for identifying grade combo boxes */
		var tmpCombos = ['MATH?','SCI?','SOC?','ENG?','LIFE?','LANG?','AGRI?','EG?1', 'EG?2', 'EG?3', 'EG?4'];	
		combos = [];
		
		for(var i = 1; i <= totalSections ; i++) {
			var fields = [];
			for( var j = 0; j < tmpCombos.length; j++) {
				// check to conform field names "scanned" table fields
				
				/* code uncommented in order to work with scanned table 
				* uncomment to work with apsssce table
				*/
				
				if( j < 7 || i > 1 ) {
					fields[j] = tmpCombos[j].replace('?', i);
				} else {
					fields[j] = tmpCombos[j].replace('?', '');
				}

				document.getElementById( fields[j] ).disabled = true;
			}
			
			combos[i] = fields;
		}
		
		/* create temporary array for identifying subject combo boxes */
		var tmpSubs = ["EE?1", "EE?2", "EE?3", "EE?4"];	
		elecSubs = [];
		
		for(var i = 1; i <= totalSections ; i++) {
			var fields = [];
			
			for(var j = 0; j < tmpSubs.length; j++) {
				// check to conform field names "scanned" table fields
				if( i > 1 ) {
					fields[j] = tmpSubs[j].replace('?',i);
				} else {
					fields[j] = tmpSubs[j].replace('?','');
				}
				
				document.getElementById( fields[j] ).disabled = true;
			}
			
			elecSubs[i] = fields;
		}
	}
	
	function setChosenGrades() {
		var gradesCombo = document.getElementById("chosenGrades");
		
		for(var x = 1; x <= totalSections ; x++) {
			var examType = document.getElementById("ExamType" + x).value;
			initComboBoxesFor( examType, x );
			
			for(var i = 0; i < gradesCombo.options.length; i++) {
				for(var j = 0; j < combos[x].length; j++) {
					//document.write( combos[x][j] + " ");
					if( combos[x][j] == gradesCombo.options[i].text ) {
						document.getElementById(combos[x][j]).value = gradesCombo.options[i].value;
					}	
				}
			}
			
			displayCalculatedGrades(x);
		}
	}
	
	function displayCalculatedGrades(section) {
		document.getElementById("totalGrade" + section).value = getTotalGrade(section);	
		document.getElementById("bestSix" + section).value = getBestSix(section);
	}
	
	function getTotalGrade(section) {
		var totalGrade = 0;
		
		for( var i = 0; i < combos[section].length; i++ ) {
			var comboBox = document.getElementById( combos[section][i] );	
			var chosenGrade = comboBox.options[ comboBox.selectedIndex ].value;
			var gradeValue = getGradeValue( chosenGrade );
			
			if( !isNaN(gradeValue) ) {
				totalGrade += gradeValue;
			}
		}
		
		return totalGrade;
	}
	
	function getBestSix(section) {
		var bestSix = 0;
		var bestElecGrades = [];
		var electiveCourses = ["EG"];
		
		bestSix += getComboGradeValue( document.getElementById("MATH" + section) );
		bestSix += getComboGradeValue( document.getElementById("SCI" + section) );
		bestSix += getComboGradeValue( document.getElementById("ENG" + section) );
		
		for( var i = 0, j = 0; i < combos[section].length; i++ ) {
			if( combos[section][i].indexOf( electiveCourses[0] ) != -1 ) {		
				bestElecGrades[j++] = getComboGradeValue( document.getElementById( combos[section][i] ) );
			}
		}
		
		bestElecGrades.sort( numerically );
		
		for(var i = 0; i < totalSections ; i++) {
			if( bestElecGrades[i] != null && !isNaN(bestElecGrades[i]) ) {
				bestSix += bestElecGrades[i];	
			}
		}
		
		return bestSix;
	}
	
	function getComboGradeValue(comboBox) {
		var chosenGrade = comboBox.options[ comboBox.selectedIndex ].value;
		var gradeValue = getGradeValue( chosenGrade );
		
		return isNaN(gradeValue) ? 0 : gradeValue;
	}
	
	function numerically(a, b) {
		if( isNaN(a) ) {
			return 1;	
		} else if( isNaN(b) ) {
			return -1;	
		}
		
		return a - b
	}
	
	function getGradeValue(gradeLetter) {
		for(var i = 0; i < grades.length; i++) {
			if( grades[i] == gradeLetter ) {
				return i;	
			}	
		}

        return 0;
	}
	
	function getNoOfSittings() {
		var no_of_sits = -1;

        // safety code to ensure the number of sittings can actually be calculated
		var comboBox = document.getElementById("ExamType1");
        
        if( comboBox == null ) {
            return no_of_sits;
        } else {
            no_of_sits = 0;
        }

		for(var i = 1; i <= combos.length; i++) {
			comboBox = document.getElementById("ExamType" + i);
			if( comboBox != null && comboBox.options[comboBox.selectedIndex].value != "" ) {
				no_of_sits++;			
			}	
		}

        //alert("No Of Sittings = " + no_of_sits);
		
		return no_of_sits;
	}
	




