///////////////////Innovation and SED form/////////////////////////

function handleInnovationFormEdit(updateSavedFlag) {
	Forms.setTabState('tabInnovationSED', 'Innovation SED', updateSavedFlag, parseFloat($('innoTotal').value) >= 0 && parseFloat($('innoSedTotal').value) >= 0);
	InnovationScorecard.calculate();
}

var InnovationScorecard = {
	calculate: function(){
		var innoTotal = this.calcInnovation();
		this.calcTotal('innoTotal', innoTotal);
		
		var sedTotal = this.calcSED();
		this.calcTotal('innoSedTotal', sedTotal);
	},
		
	calcScore: function(select, target){
		var val = $F(select);
		switch(""+val){			
			case '0':
				return 0;
			case '1':
				return 100;
			case '':
			default:
				return NaN;
		}		
	},
	calcPoints: function(score, target, weight){
		return score/target*weight;
	},
	calcItem: function(select, score, points, target, weight){
		var scoreVal = this.calcScore(select, target);
		var pointsVal = this.calcPoints(scoreVal, target, weight);
		
		$(score).update(scoreVal.toPerc(2));
		$(points).value = pointsVal.toDec(2);
		$(points+'P').update(pointsVal.toDec(2));		
		
		return pointsVal;
	},
	
	calcInnovation: function(){
		var target = 100;
		var weight = 10;
		
		var totals = [];
		totals.push(this.calcItem('innoWasteProg', 'innoWastePerc', 'innoWasteTotal', target, weight));
		totals.push(this.calcItem('innoWaterProg', 'innoWaterPerc', 'innoWaterTotal', target, weight));
		totals.push(this.calcItem('innoEnergyProg', 'innoEnergyPerc', 'innoEnergyTotal', target, weight));
		totals.push(this.calcItem('innoProcureProg', 'innoProcurePerc', 'innoProcureTotal', target, weight));
		
		return totals;
	},
	calcSED: function(){
		var target = 100;
		var weight = 10;
		
		var totals = [];
		totals.push(this.calcItem('sedOutreach', 'innoOutreachPerc', 'innoOutreachTotal', target, weight));
		totals.push(this.calcItem('sedSMMEDev', 'innoSMMEPerc', 'innoSMEETotal', target, weight));
		
		return totals;
	},
	
	calcTotal: function(element, points){
		var total = 0;
		points.each(function(val){ total += val; });
		$(element).value = total.toDec(2);
		$(element+'P').update(total.toDec(2));
	}
};
