STATS FIX by Habman

Description:
This script fixes both the MFL Rosters w/ stats page and NFL Stats page (REPORTS > NFL > NFL Team Stats). The default pages are unusable in their current form.

Examples:
Default Page
Modified Page
Implementation:
The following script should be placed in a home page message designated as footer.
<script>

function expandPlayer(thisPosition,thisPlayer,expand) {

	if(expand) {

		jQuery("#expandTotal_"+thisPosition+"_"+thisPlayer).hide();

		jQuery("#expandAvg_"+thisPosition+"_"+thisPlayer).hide();

		jQuery("#collapseTotal_"+thisPosition+"_"+thisPlayer).show();

		jQuery("#collapseAvg_"+thisPosition+"_"+thisPlayer).show();

		jQuery(".nflStatsWeekRow_"+thisPosition+"_"+thisPlayer).show();

	} else {

		jQuery("#expandTotal_"+thisPosition+"_"+thisPlayer).show();

		jQuery("#expandAvg_"+thisPosition+"_"+thisPlayer).show();

		jQuery("#collapseTotal_"+thisPosition+"_"+thisPlayer).hide();

		jQuery("#collapseAvg_"+thisPosition+"_"+thisPlayer).hide();

		jQuery(".nflStatsWeekRow_"+thisPosition+"_"+thisPlayer).hide();

	}

}

function doNFLTotal(isTotal){

	if(isTotal) {

		jQuery("#nflTotalInactive").hide();

		jQuery("#nflAvgInactive").show();

		jQuery("#nflTotalActive").show();

		jQuery("#nflAvgActive").hide();

		jQuery(".nflStatsTotalRow").show();

		jQuery(".nflStatsAvgRow").hide();

	} else {

		jQuery("#nflTotalInactive").show();

		jQuery("#nflAvgInactive").hide();

		jQuery("#nflTotalActive").hide();

		jQuery("#nflAvgActive").show();

		jQuery(".nflStatsTotalRow").hide();

		jQuery(".nflStatsAvgRow").show();

	}

}

function applyStat(thisStat,decimal) {

	if(thisStat=="-") 

		return thisStat; 

	else if(isNaN(parseFloat(thisStat))) 

		if(decimal)

			return "0.0";

		else 

			return 0;

	else 

		return thisStat;

}

function shortenHeader(thisHeader){

	if(thisHeader=="Targets") return "<span title='"+thisHeader+"'>Tgt</span>";

	if(thisHeader=="Pass Interceptions Thrown") return "<span title='"+thisHeader+"'>Int</span>";

	if(thisHeader=="Receptions") return "<span title='"+thisHeader+"'>Rcpt</span>";

	var isRush=false, isRec=false;

	if(thisHeader.indexOf("Rush")>-1) isRush=true;

	if(thisHeader.indexOf("Receiving")>-1) isRec=true;



	var newHeader = thisHeader.toUpperCase();

	newHeader = newHeader.replace("EXTRA","X");

	newHeader = newHeader.replace("NUMBER OF","#");

	newHeader = newHeader.replace("(","( ");

	newHeader = newHeader.replace(")"," )");

	var headerSplit=newHeader.split(" ");

	var newHeader="";

	for (var i=0;i<headerSplit.length;i++){

		newHeader+= headerSplit[i].substring(0,1);

	}

	if(isRush) newHeader = newHeader.replace("R","Run");

	if(isRec) newHeader = newHeader.replace("R","Rec");

	return "<span title='"+thisHeader+"'>"+newHeader+"</span>";

}

function getNFLScheduleReformat(thisTeam){

	var nflScheduleReformat = new Array();

	for (var i=1;i<22;i++) {

		var url = baseURLDynamic+"/"+year+"/export?TYPE=nflSchedule&W="+i+"&JSON=1";

		jQuery.ajax({type: 'GET',url: url, async:false}).done(function (scheduleData) {

			var teamFound = false;

			try {

				for (var m=0;m<scheduleData.nflSchedule.matchup.length;m++){

					if(scheduleData.nflSchedule.matchup[m].team[0].id==thisTeam) {

						nflScheduleReformat[i]=thisTeam+" "+scheduleData.nflSchedule.matchup[m].team[0].score+" at "+scheduleData.nflSchedule.matchup[m].team[1].score+" "+scheduleData.nflSchedule.matchup[m].team[1].id;

						teamFound=true;

						break;

					}

					if(scheduleData.nflSchedule.matchup[m].team[1].id==thisTeam) {

						nflScheduleReformat[i]=thisTeam+" "+scheduleData.nflSchedule.matchup[m].team[1].score+" vs "+scheduleData.nflSchedule.matchup[m].team[0].score+" "+scheduleData.nflSchedule.matchup[m].team[0].id;

						teamFound=true;

						break;

					}

				}

			} catch(er) {}	

			if(!teamFound)nflScheduleReformat[i]="bye";

		});

	}

	return nflScheduleReformat;

}

function doNFLStatsTable(thisTable,nflScheduleArray){

	try {

		var tableCaptionHTML = thisTable.find("caption").html();

		var thisPosition = thisTable.find("caption span").html();

		var tableRows = new Array();

		var weekRow = new Array();

		//EACH TABLE HAS ONE ROW OF TH WITH WEEK INFORMATION

		thisTable.find("tr:eq(0) th").each(function(){

			weekRow[weekRow.length] = jQuery(this).html();

		});

		//EACH TABLE HAS NEW PLAYER BEGIN WHERE COLSPAN ATTRIBUTE EXISTS

		var currPlayer=0;

		var currRow = 0;

		var rowCount = 0;

		thisTable.find("tr").each(function(){

			if(rowCount>0) { // we want to ignore first row

				if(jQuery(this).find("th").length>0) { // NEW PLAYER SETUP

					currPlayer = tableRows.length;

					if(jQuery(this).find("th").html().indexOf("<img")>0)

						tableRows[currPlayer] = ({"name":jQuery(this).find("th").html().substring(0,jQuery(this).find("th").html().indexOf("<img")),"hasInjury":false,"playerRows":new Array()});

					else

						tableRows[currPlayer] = ({"name":jQuery(this).find("th").html(),"hasInjury":false,"playerRows":new Array()});

				} else {

					if(jQuery(this).find("td:eq(0)").html()=="Injury Status") tableRows[currPlayer].hasInjury=true;

					currRow = tableRows[currPlayer].playerRows.length;

					tableRows[currPlayer].playerRows[currRow] = new Array();

					var currCol=0;

					jQuery(this).find("td").each(function(){

						tableRows[currPlayer].playerRows[currRow][currCol] = jQuery(this).html();

						currCol++;

					});

				}

			}

			rowCount++;

		});

		var newTableContent = "";

		if(document.getElementById("body_nfl_team_stats"))

			if(tableRows[0].hasInjury) var statsLastRow = tableRows[0].playerRows.length - 1; else var statsLastRow = tableRows[0].playerRows.length;

		else

			if(tableRows[0].hasInjury) var statsLastRow = tableRows[0].playerRows.length - 2; else var statsLastRow = tableRows[0].playerRows.length - 1;

		

		newTableContent+="<caption>"+tableCaptionHTML+"</caption><tbody>";

		//HEADER ROW

		newTableContent+="<tr><th>Player</th><th>Week</th>";

		if(tableRows[0].hasInjury) newTableContent+="<th>Injury</th>";

		for (var i=0;i<statsLastRow;i++){

			newTableContent+="<th style='text-align:center'>"+shortenHeader(tableRows[0].playerRows[i][0])+"</th>";

		}

		newTableContent+="</tr>";

		for(var p=0;p<tableRows.length;p++) {

			//TOTALS ROW (2ND LAST COLUMN)

			newTableContent+='<tr class="oddtablerow nflStatsTotalRow"><td><span id="expandTotal_'+thisPosition+'_'+p+'" onclick="expandPlayer(\''+thisPosition+'\',\''+p+'\',true)"><i class="fa fa-plus-square stats-expand" aria-hidden="true"></i></span><span id="collapseTotal_'+thisPosition+'_'+p+'" style="display:none" onclick="expandPlayer(\''+thisPosition+'\',\''+p+'\',false)"><i class="fa fa-minus-square stats-collapse" aria-hidden="true"></i></span>'+tableRows[p].name+'</td><td style="text-align:center">Total</td>';

			if(tableRows[p].hasInjury) newTableContent+="<td style='text-align:center'></td>";

			for (var i=0;i<statsLastRow;i++){

				if(!(tableRows[0].hasInjury&&i==(tableRows[0].playerRows.length-1))) newTableContent+="<td class='points' style='text-align:center'>"+applyStat(tableRows[p].playerRows[i][tableRows[p].playerRows[i].length-2],false)+"</td>";

			}

			newTableContent+="</tr>";

			//AVG ROW (LAST COLUMN)

			newTableContent+='<tr class="oddtablerow nflStatsAvgRow"><td><span id="expandAvg_'+thisPosition+'_'+p+'" onclick="expandPlayer(\''+thisPosition+'\',\''+p+'\',true)"><i class="fa fa-plus-square stats-expand" aria-hidden="true"></i></span><span id="collapseAvg_'+thisPosition+'_'+p+'" style="display:none" onclick="expandPlayer(\''+thisPosition+'\',\''+p+'\',false)"><i class="fa fa-minus-square stats-collapse" aria-hidden="true"></i></span>'+tableRows[p].name+'</td><td style="text-align:center">Avg</td>';

			if(tableRows[p].hasInjury) newTableContent+="<td style='text-align:center'></td>";

			for (var i=0;i<statsLastRow;i++){

				if( !( tableRows[0].hasInjury&&i==(tableRows[0].playerRows.length-1) ) ) newTableContent+="<td class='points' style='text-align:center'>"+applyStat(tableRows[p].playerRows[i][tableRows[p].playerRows[i].length-1],true)+"</td>";

			}

			newTableContent+="</tr>";

			//WEEKS

			for (w=1;w<tableRows[p].playerRows[0].length-2;w++){

				if(document.getElementById("body_nfl_team_stats"))

					newTableContent+="<tr class='eventablerow nflStatsWeekRow nflStatsWeekRow_"+thisPosition+"_"+p+"' style='display:none'><td style='text-align:right'>"+nflScheduleArray[w]+"</td><td style='text-align:center'>"+w+"</td>";

				else

					newTableContent+="<tr class='eventablerow nflStatsWeekRow nflStatsWeekRow_"+thisPosition+"_"+p+"' style='display:none'><td style='text-align:right'>"+tableRows[p].playerRows[tableRows[p].playerRows.length-1][w]+"</td><td style='text-align:center'>"+w+"</td>";				

				for (var i=0;i<statsLastRow;i++){

					if(tableRows[p].hasInjury&&i==0) {

						if(document.getElementById("body_nfl_team_stats"))

							newTableContent+="<td style='text-align:center'>"+tableRows[p].playerRows[tableRows[p].playerRows.length-1][w]+"</td>";

						else

							newTableContent+="<td style='text-align:center'>"+tableRows[p].playerRows[tableRows[p].playerRows.length-2][w]+"</td>";

					}

					if(!(tableRows[0].hasInjury&&i==(tableRows[0].playerRows.length-1))) newTableContent+="<td class='points' style='text-align:center'>"+applyStat(tableRows[p].playerRows[i][w],false)+"</td>";

				}

				newTableContent+="</tr>";

			}

		}

		newTableContent+="</tbody>";

		thisTable.html(newTableContent);

		if(!reformattedStatsFound) thisTable.closest("div").before("Showing: <span id='nflTotalInactive' style='display:none'><a href='#total' onclick='doNFLTotal(true)'>Total</a></span><span id='nflTotalActive'>Total</span> | <span id='nflAvgInactive'><a href='#avg' onclick='doNFLTotal(false)'>Avg</a></span><span id='nflAvgActive' style='display:none'>Avg</span><br><br>");

		reformattedStatsFound = true;

		jQuery(".nflStatsAvgRow").hide();

	} catch(er){}

}

function doMFL_reformatStatsPage(){

	var nflScheduleTemp = getNFLScheduleReformat(jQuery("select[name='TEAM'] option:selected").text());

	jQuery("body").find("table.report").each(function(){doNFLStatsTable(jQuery(this),nflScheduleTemp);});

	nflScheduleTemp = null;

}

if( (document.getElementById('body_nfl_team_stats')||document.getElementById('body_options_138')) && location.href.indexOf("#DEFAULT")==-1) {

	var reformattedStatsFound = false;

	doMFL_reformatStatsPage()

}

</script>
Consider Donating:
These apps are all 100% free to use however if you want to show your appreciation for the time and effort it took to create then these apps then please consider donating.

Thank you.