// TEAM FUNCTIONS (STORED PROCEDURES)
// sp_teams.js
// NB: NEEDS database.js

// NOTE:
// TO DISPLAY THE TEAM FOR A MATCH CALL f_team(Key) IN sp_scores.js

// :: FUNCTIONS
// f_rtnTeamLegend() - Returns simple list of teams 
// f_rtnTeams(SeasonID)

// Returns simple list of teams as a legend
// FloatDiv - none, left, right
function f_rtnTeamLegend(FloatDiv) {
	var i, t, tarr;
	
	// Init
	t = '<div style="float:' + FloatDiv + ';margin:15px;padding:5px;border:1px solid #666666;color:#666666;background-color:#ffffff;">';
	t += '<div class="smallfont" style="background-color:#eeeeee;padding-left:5px;padding-bottom:2px;border:1px solid #666666;margin-bottom:5px;font-weight:bold;">Team Legend...</div>';
	
	for (i=0; i<(TeamArr.length - 1); i++) {
		tarr = TeamArr[i].split(',');
		t += '<b>' + tarr[0] + '</b> - ' + tarr[1] + '<br />';
	}
	t += '</div>';
	
	return t;
}

// RETURNS TEAMS AND PLAYERS FOR PASSED SEASON ID
// TEAMS ARRANGED 3-WIDE
// 20/3/2006 - Can specify _ALL to display all teams across all seasons!
function f_rtnTeams(SeasonID) {
  var i, j, k, t, tparr, parr, count, colcount, pcount, prevs, nexts;
  var _NOWIDE;

  // INIT
  _NOWIDE = 3;	// NO COLS WIDE
  count = 0;
	colcount = 0;
  t = '';	
	prevs = '';		// Previous Season
	nexts = '';		// Next Season (lookahead)

  // STEP THROUGH TEAM PLAYERS
  for (i=0; i<TeamPlayersArr.length; i++) {
    tparr = TeamPlayersArr[i].split(',');
    if ((SeasonID != _ALL) && (tparr[0] != SeasonID)) {
      continue;
    }
		
		count++;		
		colcount++;
		
		// New Season?
		// NB: At least 1 - Could be more than 1 season if _ALL
		if (prevs != tparr[0]) {
			prevs = tparr[0];
			if (SeasonID == _ALL) {
				t += '<div class="heading">' + f_getSeason(prevs) + '</div>';
			}
		  t += '<TABLE WIDTH="100%" CELLPADDING=0 CELLSPACING=0 border="0"><TR>';
			colcount = 0;			
		}
    // START NEW ROW?
    else if (colcount == _NOWIDE) {
			colcount = 0;
      t += '</TR>';
      t += '<TR><TD COLSPAN=' + _NOWIDE + '>&nbsp;</TD></TR>';
      t += '<TR>';
    }		

    // BEGIN: POS CELL
    t += '<TD STYLE="text-align:left;">';

    // TEAM PLAYERS...
    t += '<TABLE CLASS="fixtures" CELLPADDING=0 CELLSPACING=0>';
    t += '<TR><TH>' + f_lookupTeamName(tparr[1]) + ' <SPAN CLASS="smallfont faint">(' + tparr[1] + ')</SPAN></TH>';
    t += '<TH STYLE="text-align:right;color:#CCCCFF;">';
    if (tparr[2] != 'x') {
      t += 'Div ' + tparr[2];
    } else {
      t += '&nbsp;';
    }
    t += '</TH></TR>';

    // STEP THROUGH PLAYERS
    pcount = 0;
    j = 3;		// 1st FIELD WITH PLAYER ID
    while (typeof(tparr[j]) != 'undefined') {
      pcount++;
      parr = tparr[j].split('-');
      t += '<TR><TD COLSPAN=2><A CLASS="nounderline" HREF="javascript:f_popupPerson(\'' + parr[0] + '\');">' + f_getPersonName(parr[0]) + '</A>';
			for (k=1; k<parr.length; k++) {
	      if (parr[k] == 'C') {
	        t += ' <B>(Capt)</B>';
	      } else if (parr[k] == 'R') {
	        t += ' <SPAN CLASS="faint" style="font-style:italic;">(<abbr title="Reserve Player">Res</abbr>)</SPAN>';
	      } else if (parr[k] == 'SC') {
	        t += ' <span>(Sen. Capt)</span>';
				} else {
					// Any other character
					t += ' <span>('+parr[k]+')</span>';
				}
			}

      t += '</TD></TR>';

      j++;
    }
    // NO PLAYERS?
    if (pcount == 0) {
      t += '<TR><TD COLSPAN=2><I>(No team selected!)</I></TD></TR>';
    }
    
    t += '</TABLE>';

    // END: POS CELL
    t += '</TD>';
		
		// Close Table ...?
		// Look ahead at next record...
		if (i == (TeamPlayersArr.length - 1)) {
			nexts = '';
		} else {
	    tparr = TeamPlayersArr[i+1].split(',');
			nexts = tparr[0]; 
    }
		if (nexts != prevs) {
			t += '</TR></TABLE>';
		}
  }

  // NO RECORDS?
  if (count == 0) {
    t = '<DIV CLASS="alert">There are no records for the specified season.</DIV>';
  }

  return t;
}
