window.MaxStandardsScore= 4;
window.GradeLevelMaxScore = 4;
window.A1Weight= 0.25;
window.A2Weight= 0.75;
window.A1FieldSuffixes= [ '-1A', '-1B', '-1C', '-1D' ];
window.A2FieldSuffixes= [ '-2', '-3', '-4' ];
window.RubricTotalName= '-total_grade';
window.AssessTotalName= '-total_perc';
window.student_totals= new Object();
									 
function calc_average(a, max_score) {
	var denominator= 0;
	var numerator= 0;
	for (var i in a) {  
      t= a[i]+"";
		if (t.length<1){  // blank entries are not calculated
			continue;
		} else {
			n= parseInt(t);
			if (isNaN(n) || n < 0) continue;  // non-ints or negative ints are not calculated 
			if (n > max_score) n= max_score; // max_score ceiling check.
			denominator+= max_score;
			numerator+= n;
		}
	}
	
	if (denominator==0) {  // no score was calculated - all blanks
		return '';
	} else {
		return numerator/denominator;
	}
}

// Normalize the score entered (must be an empty string, or an integer from 0 to MaxStandardsScore)
function normalize_score(v) {
		v= v+"";
		if (v!='undefined' || v.length>0) {
			v= parseInt(v);
			if (isNaN(v)) {
				v= '';
			} else if (v>window.MaxStandardsScore) {
				v= window.MaxStandardsScore;
			} else if (v<0) {
				v= 0;	
			}
		} else if (v=='undefined') {
			v= '';	
		}	
		return v;
}
function normalize_score_array(a) {
	for (i=0; i<a.length; i++) {
		a[i]= normalize_score(a[i]);	
	}
	return a;
}

// Returns true if the variable passed is an empty string
function is_empty_string(v) {
	return v.toString().length==0;	
}

// Calculate a student's average score for the given max_score ceiling.
// Rubric scores can always be in the range of undefined (or blank ''), 0 to MaxStandardsScore.
// Percentage or passing scores are in the range of 0 to GradeLevelMaxScore (aka, the assessment goal)
// This function returns an empty string '' or a number in the range of 0 to 1.
// a1 is weighed as 25% of the average
// a2 is weighed as 75% of the average
function calc_student_avg(a1, a2, max_score) {
	var avg= 0.0;
	a1= normalize_score_array(a1);
	a2= normalize_score_array(a2);
	a1_avg= calc_average(a1, max_score);
	a2_avg= calc_average(a2, max_score);
	
	// An "empty" average should not be considered when calculating the student's total average
	if (!is_empty_string(a1_avg) && !is_empty_string(a2_avg)) {
		avg= 	(a1_avg * window.A1Weight) + (a2_avg * window.A2Weight); 
	} else if (is_empty_string(a1_avg)) {
		avg= a2_avg;
	} else {
		avg= a1_avg;
	}
	
	return avg;
}

// frm - form object that contains the student score boxes
// prefix - name prefix of the student's score boxes
function update_avg(frm, prefix) {
	var a1= init_score_array(frm, prefix, window.A1FieldSuffixes);
	var a2= init_score_array(frm, prefix, window.A2FieldSuffixes);
	
	// Average for rubric score
	var rubric_avg= calc_student_avg(a1, a2, window.MaxStandardsScore);
	
	// If the assessment goal differs from the max rubric score, calculate it seperately.
	var assess_avg= (window.MaxStandardsScore > window.GradeLevelMaxScore) ? calc_student_avg(a1, a2, window.GradeLevelMaxScore) : rubric_avg;

	// Display the rubric and assess totals
	var field_name= prefix + window.RubricTotalName;
	if (typeof frm[field_name]!='undefined') {
		if (is_empty_string(rubric_avg)) {
			frm[field_name].value= '';
		// Calculate rubric score as number in the range of 0 to 4
		} else {
			frm[field_name].value= Math.round(100 * rubric_avg * window.MaxStandardsScore) / 100;
		}
	}
	field_name= prefix + window.AssessTotalName;
	if (typeof frm[field_name]!='undefined') {
		if (is_empty_string(assess_avg)) {
			frm[field_name].value= '';
		// Calculate rubric score as number in the range of 0 to 4
		} else {
			frm[field_name].value= Math.round(100 * assess_avg) + "%";
		}
	}
}

// Grab the scores from form frm, identified by prefix and a_source combos.
function init_score_array(frm, prefix, a_source) {
	var a= new Array();
	var field_name= '';
	var o= '';
	for (i=0; i<a_source.length; i++) {
		field_name= prefix + a_source[i];
		if (typeof frm[field_name]!='undefined') {
			a[i]= frm[field_name].value;
		} else {
			a[i]= '';	
		}
	}
	return a;
}


/*
	// Save the student's average score (on the 0-4 rubric scale) for later user
	if (typeof student_id!='undefined') {
		if (typeof window.student_totals[student_id]=='undefined') 	window.student_totals[student_id]= new Array();
		if (display_avg!=='') window.student_totals[student_id].push(display_avg);
	}
*/

/*
// Display the average score and % for a student for all units
// Units that are not completed are not considered when calculating this average.
function update_student_total(frm, student_id) {
	if (typeof window.student_totals[student_id]!='undefined' && window.student_totals[student_id].length > 0) {
		var student_avg= CalculateAverage(window.student_totals[student_id]);
		var student_avg_display= Math.round(student_avg*window.MaxStandardsScore*100)/100;
		if (student_avg_display >= window.GradeLevelMaxScore) {
			frm['student_total_perc_' + student_id].value= '100%';							
		} else {
			grade_level_ratio= window.GradeLevelMaxScore / window.MaxStandardsScore;
			grade_level_avg= student_avg / grade_level_ratio;
			frm['student_total_perc_' + student_id].value= Math.round(grade_level_avg*100) + '%';			
		}
		frm['student_total_' + student_id].value= student_avg_display;
	}
}

// Tally the student scores and update total
function update_admin_totals(frm) {
	var scores= 0;
	var student_count= 0;
	var students_passing= 0;
	for (var i in window.student_totals) {
		student_count++;
		var student_id= i + "";
		var current_student_score= parseFloat(frm['student_total_' + student_id].value);
		if (!isNaN(current_student_score)) {
			scores+= current_student_score;
			if (current_student_score >= window.GradeLevelMaxScore) students_passing++;
		}
	}
	var avg= scores / student_count;
	frm['class_total'].value= Math.round(avg*100)/100;
	frm['class_total_perc'].value= Math.round((avg / window.GradeLevelMaxScore)*100) + '%';
	frm['passing_students'].value= students_passing;
	frm['total_students'].value= student_count;
	frm['passing_perc'].value= Math.round((students_passing / student_count)*100) + '%';
}
*/



