//-----------------------------------------------------------------------------
// CSLAMetrics.js :: used by SLA Metrics access widget
//-----------------------------------------------------------------------------
/*
	METRICS FORMAT:
	------------------------
	SLA.addMonthlyMetrics({
		year: 2003,
		month: 9,
		target: {
			latency: 50,
			loss: 0.3,
			avail: 99.97,
			kb40: 2.503
		},
		actual: {
			latency: 31,
			loss: 0.021,
			avail: 99.999,
			kb40: 2.03
		}
	});
*/
CSLAMetrics = function(){ this.init(); }
CSLAMetrics.prototype.init = function (){
	this.metainfo_list = {
		avail:{
			label:'Network Availability',
			units:'%',
			direction:'>'
		},
		latency:{
			label:'Latency (roundtrip)',
			units:' ms',
			direction:'<'
		},
		loss:{
			label:'Packet Loss',
			units:'%',
			direction:'<'
		},
		kb40:{
			label:'KB40*',
			units:' sec',
			direction:'<'
		}
	};

	this.csstag_list = {good:'statgood', bad:'statpoor'};
	this.monthlymetrics_list = new Array();

	this.monthNamesShort = new Array(
		"JAN", "FEB", "MAR",
		"APR", "MAY", "JUN",
		"JUL", "AUG", "SEP",
		"OCT", "NOV", "DEC"
	);
	this.monthNamesLong = new Array(
		"JANUARY", "FEBRUARY", "MARCH",
		"APRIL", "MAY", "JUNE",
		"JULY", "AUGUST", "SEPTEMBER",
		"OCTOBER", "NOVEMBER", "DECEMBER"
	);

	this.targetDivId = 'sla_metrics';
	this.object_name = 'SLA';
}

CSLAMetrics.prototype.setLabels = function (labels_list){
	this.labels_list = labels_list;
}
CSLAMetrics.prototype.setUnits = function (units_list){
	this.units_list = units_list;
}
CSLAMetrics.prototype.setGoodDirection = function (targetdirection_list){
	this.targetdirection_list = targetdirection_list;
}
CSLAMetrics.prototype.setMinPrecision = function (minprecision_list){
	this.minprecision_list = minprecision_list;
}
CSLAMetrics.prototype.setTargetDivId = function (targetDivId){
	this.targetDivId = targetDivId;
}
CSLAMetrics.prototype.setObjectName = function (object_name){
	this.object_name = object_name;
}

CSLAMetrics.prototype.addMonthlyMetrics = function (metrics_object){
	this.monthlymetrics_list.push(metrics_object);
}
CSLAMetrics.prototype.getMonthID = function (year, month){
	for (var i = 0; i < this.monthlymetrics_list.length; i++){
		var metrics = this.monthlymetrics_list[i];
		var listed_year = metrics.year;
		var listed_month = metrics.month;

		if (year == listed_year && month == listed_month){ return i; }
	}

	return -1;
}
CSLAMetrics.prototype.getMonthlyMetrics = function (monthID){
	return this.monthlymetrics_list[monthID];
}
CSLAMetrics.prototype.getMetricsCount = function (){
	return this.monthlymetrics_list.length;
}
CSLAMetrics.prototype.getLayoutText = function (year, month){
	var num_months = this.getMetricsCount();

	while (num_months >= 0){
		var monthID = this.getMonthID(year, month);
		if (monthID != -1){ return this.getLayoutForMonth(monthID); }

		month--;
		num_months--;
		if (month < 0){ month = 11; year--; }

	}
}
CSLAMetrics.prototype.getHeaderStrip = function (month, year){
	//-----------------------------------------------------------
	//  DRAW THAT LITTLE BLUE ROW ABOVE THE STATS TABLE
	//-----------------------------------------------------------
	var output = '';

	output += '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>';

	for (monthID = 0; monthID < 12; monthID ++) {
		var month_abbr = this.monthNamesShort[monthID];
		var metrics_exists = this.getMonthID(year, monthID);

		//ORDER of appraisal:  IF current month, ELSE IF any other month with data, ELSE not available
		if (month == monthID) {
			output += '<TD class="monthBlack">';
			output += '<div style="color:white; font-weight:bold;" align="center">';
			output += '<acronym title="This is the currently viewed month [' + month_abbr + ' ' + year + ']">';
			output += month_abbr;
			output += '</acronym>';
			output += '</div></TD>';
		}
		else if (metrics_exists != -1) {
			output += '<TD class="monthAvail">';
			output += '<div  class="monthAvail" align="center">';
			output += '<acronym title="Click to view metrics for ' + month_abbr + ' ' + year + '">';
			output += '<a style="color:white; font-weight:normal; text-decoration:none;" href="javascript:' + this.object_name + '.' + 'writeDiv(' + monthID + ',' + year + ');">';
			output += month_abbr + '</a>';
			output += '</acronym>';
			output += '</div></TD>';
		}
		else if (month < monthID){
			output += '<TD class="monthNIL">';
			output += '<div align="center">';
			output += '<acronym title="Metrics are not available for this month at this time">';
			output += month_abbr;
			output += '</acronym>';
			output += '</div></TD>';
		}
		else {
			output += '<TD class="monthNIL">';
			output += '<div align="center">';
			output += '<acronym title="Metrics are not available for this month">';
			output += month_abbr;
			output += '</acronym>';
			output += '</div></TD>';
		}
	}

	output += '</tr></table>';

	return output;
}
CSLAMetrics.prototype.getGoodBadClass = function (actual, target, direction){
	if (direction == '>'){
		if (actual >= target){ return this.csstag_list.good; }
		else { return this.csstag_list.bad; }
	}
	else {
		if (actual <= target){ return this.csstag_list.good; }
		else { return this.csstag_list.bad; }
	}
}
CSLAMetrics.prototype.getSectionHeader = function (month, year){
	var output = '';
	var month_name = this.monthNamesLong[month];

	output += '<table class="calendarView" width="100%">';
	output += '<tr><td colspan="3">';

	output += this.getHeaderStrip(month, year);

	output += '</td></tr>';

	output += '<tr><td class="section_label">Statistics for ' + month_name + ' ' + year;
	output += '<td class="subsection" ><div style="color:white;" align="center">TARGET</div></td>';
	output += '<td class="subsection"><div style="color:white;" align="center">ACTUAL</div></td></tr>';

	return output;
}
CSLAMetrics.prototype.getYearsList = function (){
	var last_year = null;
	var years_list = new Array();

	for (var monthID = 0; monthID < this.monthlymetrics_list.length; monthID++){
		var metrics = this.getMonthlyMetrics(monthID);
		var year = metrics.year;

		if (year != last_year){ years_list.push(year); last_year = year;}
	}

	return years_list;
}
CSLAMetrics.prototype.getSectionFooter = function (current_year){
	var output = '';
	var years_list = this.getYearsList();

	output += '</table>';
	output += '<table><tr><td>&nbsp;</td></tr></table>';
	output += '<table class="calendarView" width="100%">';
	output += '<tr>';
	output += '<td class="monthAvail">Archived Years</td>';

	for (var yearID = 0; yearID < years_list.length; yearID++){
		var year = years_list[yearID];

		if (current_year != year){
			output += '<td align="center" class="monthAvail">';
			output += '<acronym title="Click to view archived metrics for ' + year + '">';
			output += '<a style="color:black; font-weight:normal; text-decoration:none;" href="javascript:' + this.object_name + '.' + 'writeDiv(' + 11 + ',' + year + ');">';
			output += year + '</a>';
			output += '</acronym>';
			output += '</td>';
		}
	}

	output += '</tr>';
	output += '</table>';

	return output;
}
CSLAMetrics.prototype.getRowText = function (actual, target, result, label, units){
	var output = '';

	output += '<tr>';
	output += '<td class="textbody2"><b>' + label + '</b></td>';
	output += '<td nowrap  class="textbody2"> <div class="datum" style="color:black;" >' + target + units + '</div></td>';
	output += '<td nowrap  class="' + result +'">';
	output += '<div class="datum" style="color:black;">' + actual + units + '</div></td>';
	output += '</tr>';

	return output;
}
CSLAMetrics.prototype.getLayoutForMonth = function (monthID) {
	//-----------------------------------------------------------
	//  DRAW THE STATS TABLE
	//-----------------------------------------------------------

	var output = '';
	var metrics = this.getMonthlyMetrics(monthID);
	var metrics_year = metrics.year;
	var metrics_month = metrics.month;

	output += this.getSectionHeader(metrics_month, metrics_year);

	for (var obj_name in this.metainfo_list){
		if (metrics.actual[obj_name] != null){
			var actual = metrics.actual[obj_name];
			var target = metrics.target[obj_name];

			var label = this.metainfo_list[obj_name].label;
			var units = this.metainfo_list[obj_name].units;
			var direction = this.metainfo_list[obj_name].direction;

			var result = this.getGoodBadClass( actual, target, direction);

			output += this.getRowText(actual, target, result, label, units);
		}
	}

	output += this.getSectionFooter(metrics_year);

	return output;
}

CSLAMetrics.prototype.writeDiv = function (month, year){
	var today = new Date();
	var month_temp = today.getMonth();
	var year_temp = today.getFullYear();

	if (month >= 0){  month_temp = month; }
	if (year >= 0){ year_temp = year; }

	var HTML = this.getLayoutText(year_temp, month_temp);

	if (is_nav6up) { document.getElementById(this.targetDivId).innerHTML = HTML; }
	else { document.all[this.targetDivId].innerHTML = HTML; }

}
