/*
 * js/bb_team.js
 * ajax interface to edit bb teamsinformation
 *
 * Joris Dormans (2007)
 *
 * requires:
 * 3dparty/jkl-parsexml.js, js/bb_general.js, js/bb_modalpopup.js
 *
 */



function BBTeam (HTMLParent, objectName, idTeam, editing, historical, historicalGame) {
  this.objectName = objectName;
  this.HTMLParent = HTMLParent;
  this.idTeam = idTeam;
  this.data;
  this.xhReqPopup = new XHReqCaller(objectName+".xhReqPopup");
  this.editing=editing;
  this.historical=historical;
  this.historicalGame=historicalGame;
  this.refreshControl();
}

BBTeam.prototype.createNewTeam = function() {
  var race = escape($(this.objectName+'race').value);
  var coach = escape($(this.objectName+'coach').value);
  var teamname = escape($(this.objectName+'teamname').value);
  
  var xml = new JKL.ParseXML("services/bb_create_team.php?r="+race+"&c="+coach+"&n="+teamname);
  this.data = xml.parse();
  if (this.data.error) {
    $(this.HTMLParent).innerHTML="<div class='error'>Error "+this.data.error.code+": "+this.data.error.message+"</div>";
    return;
  }
  this.idTeam=0;
  this.refreshControl();
}

BBTeam.prototype.clickRow = function(row) {
  document.location="team.php?i="+row;
}

BBTeam.prototype.newTeam = function(row) {
  this.idTeam=-1;
  this.refreshControl();
}

BBTeam.prototype.showTeam = function(team) {
  var tname=team.name;
  if (tname==undefined) tname=bbLanguage.noName;
  tname=unescape(tname);
  var race=unescape(team.race);
  var res="<tr id='team"+team.id_team+"' class='bb_inactiverow' onclick='"+this.objectName+".clickRow("+team.id_team+")' onmouseover=bbLight('team"+team.id_team+"') onmouseout=bbDim('team"+team.id_team+"')><td>"+tname+"</td><td>"+race+"</td></tr>";
  return res;
}

BBTeam.prototype.refreshControl = function() {
  var res="";
  this.menu = unescape(this.xhReqPopup.sGet("services/bb_menu.php?c=team&i="+this.idTeam+"&h="+this.historical));
  if (this.idTeam==-1) {
    //new Team
    var xml = new JKL.ParseXML("services/bb_coaches_of_user.php");


    res+="<div class='bb_span'>";
    res+="<h1 class='bb_header'>"+bbLanguage.teamNewCaption+"</h1>";
    res+=this.menu;
    var coaches=unescape(this.xhReqPopup.sGet("services/bb_coaches_of_user.php"));
    if (coaches=="") {
      res+="<div class='bb_content'><p>"+bbLanguage.teamCreateCoachFirst+"</p></div>";
    } else {
      res+="<div class='bb_content'><table>";
      res+="<tr><td>"+bbLanguage.teamTeamname+"</td><td><input id='"+this.objectName+"teamname'></input></td></tr>";
      res+="<tr><td>"+bbLanguage.teamCoach+"</td><td><select id='"+this.objectName+"coach'>"+coaches;
      res+="</select></td></tr>";

      var xml = new JKL.ParseXML("services/bb_active_races.php");
      this.data = xml.parse();
      if (this.data.error) {
        $(this.HTMLParent).innerHTML="<div class='error'>Error "+this.data.error.code+": "+this.data.error.message+"</div>";
        return;
      }

      res+="<tr><td>"+bbLanguage.teamRace+"</td><td><select id='"+this.objectName+"race'>";
      if (this.data.races) {
        if (this.data.races.race) {
          if (this.data.races.race.length>1) {
            for (m=0; m<this.data.races.race.length; m++) res+="<option value='"+unescape(this.data.races.race[m].id_race)+"' >"+unescape(this.data.races.race[m].race)+"</option>";
          } else {
            res+="<option value='"+unescape(this.data.races.race.id_race)+"' >"+unescape(this.data.races.race.race)+"</option>";
          }
        }
      }
      res+="</select></td></tr>";
      res+="</table>";
      res+="<div class='bb_right'><input type='button' value='"+bbLanguage.teamNewCreate+"' onclick='"+this.objectName+".createNewTeam()' /></div>";
    }
    res+="</div>"+bbFooter()+"</div>";

  } else if (this.idTeam==0) {
    //collect data

    res+="<div class='bb_span'>";
    res+="<h1 class='bb_header'>"+bbLanguage.teamListCaption+"</h1>";
    res+=this.menu;
    res+="<div class='bb_content'><table>";
    res+=unescape(this.xhReqPopup.sGet("services/bb_teamlist.php"));
    res+="</table></div>"+bbFooter()+"</div>";

  } else {
    if (this.historical) {
      res=this.showTeamHistory();
    } else {
      if (this.historicalGame>0) {
        this.xhReqPopup.sGet("services/bb_recalculate_team.php?i="+this.idTeam+"&g="+this.historicalGame);
      };
      //collect data
      var xml = new JKL.ParseXML("services/bb_team_info.php?i="+this.idTeam);
      this.data = xml.parse();
      if (this.data.error) {
        $(this.HTMLParent).innerHTML="<div class='error'>Error "+this.data.error.code+": "+this.data.error.message+"</div>";
        return;
      }
      if (this.data.team) {
        if (this.data.team.team) {
          //does the team need to be recalculated?
          if (this.data.team.team.recalculate=='Y' && this.historicalGame==0) {
            this.xhReqPopup.sGet("services/bb_recalculate_team.php?i="+this.idTeam);
            this.refreshControl();
          }
          if (this.data.team.team.isowner==1 && this.editing && this.historicalGame==0) res+=this.showOwnTeam();
          else res+=this.showOtherTeam();
        }
      }
    }
  }

  $(this.HTMLParent).innerHTML=res;
  if ($('chep')) this.enableButtons();
}

BBTeam.prototype.clickB = function(w, v) {
  if (w==1 && this.data.team.team.can_have_apo=='Y') {
    if (v>0) {
      if ($(this.objectName+'apothecary').value>=1) {
        alert(bbLanguage.alreadyHasApo);
        return;
      }
      if ($(this.objectName+'money').value<50) {
        alert(bbLanguage.notEnoughMoney);
        return;
      }
      $(this.objectName+'apothecary').value++;
      $(this.objectName+'money').value-=50;
      this.modified=true;
    } else if (v<0) {
      if ($(this.objectName+'apothecary').value<=0) {
        alert(bbLanguage.hasNoApo);
        return;
      }
      $(this.objectName+'apothecary').value--;
      if (this.noGames) $(this.objectName+'money').value-=(-50);
      this.modified=true;
    }
  }
  if (w==2) {
    if (v>0) {
      if ($(this.objectName+'coaches').value>=99) {
        alert(bbLanguage.tooManyCoaches);
        return;
      }
      if ($(this.objectName+'money').value<10) {
        alert(bbLanguage.notEnoughMoney);
        return;
      }
      $(this.objectName+'coaches').value++;
      $(this.objectName+'money').value-=10;
      this.modified=true;
    } else if (v<0) {
      if ($(this.objectName+'coaches').value<=0) {
        alert(bbLanguage.hasNoCoaches);
        return;
      }
      $(this.objectName+'coaches').value--;
      if (this.noGames) $(this.objectName+'money').value-=(-10);
      this.modified=true;
    }
  }
  if (w==3) {
    if (v>0) {
      if ($(this.objectName+'cheerleaders').value>=99) {
        alert(bbLanguage.tooManyCheerleaders);
        return;
      }
      if ($(this.objectName+'money').value<10) {
        alert(bbLanguage.notEnoughMoney);
        return;
      }
      $(this.objectName+'cheerleaders').value++;
      $(this.objectName+'money').value-=10;
      this.modified=true;
    } else if (v<0) {
      if ($(this.objectName+'cheerleaders').value<=0) {
        alert(bbLanguage.hasNoCheerleaders);
        return;
      }
      $(this.objectName+'cheerleaders').value--;
      if (this.noGames) $(this.objectName+'money').value-=(-10);
      this.modified=true;
    }
  }
  if (w==4) {
    if (v>0) {
      if ($(this.objectName+'rerolls').value>=8) {
        alert(bbLanguage.tooManyRerolls);
        return;
      }
      if (this.noGames) {
        if ($(this.objectName+'money').value<parseInt(this.data.team.team.reroll_cost)) {
          alert(bbLanguage.notEnoughMoney);
          return;
        }
        $(this.objectName+'rerolls').value++;
        $(this.objectName+'money').value-=this.data.team.team.reroll_cost;
        this.modified=true;
      } else {
        if ($(this.objectName+'money').value<2*parseInt(this.data.team.team.reroll_cost)) {
          alert(bbLanguage.notEnoughMoney);
          return;
        }
        $(this.objectName+'rerolls').value++;
        $(this.objectName+'money').value-=2*this.data.team.team.reroll_cost;
        this.modified=true;
      }
    } else if (v<0) {
      if ($(this.objectName+'rerolls').value<=0) {
        alert(bbLanguage.hasNoRerolls);
        return;
      }
      $(this.objectName+'rerolls').value--;
      if (this.noGames) $(this.objectName+'money').value-=(-this.data.team.team.reroll_cost);
      this.modified=true;
    }
  }
  if (w==5 && this.noGames) {
    if (v>0) {
      if ($(this.objectName+'fanfactor').value>=9) {
        alert(bbLanguage.tooMuchFans);
        return;
      }
      if ($(this.objectName+'money').value<10) {
        alert(bbLanguage.notEnoughMoney);
        return;
      }
      $(this.objectName+'fanfactor').value++;
      $(this.objectName+'money').value-=10;
      this.modified=true;
    } else if (v<0) {
      if ($(this.objectName+'fanfactor').value<=0) {
        alert(bbLanguage.hasNoFans);
        return;
      }
      $(this.objectName+'fanfactor').value--;
      $(this.objectName+'money').value-=(-10);
      this.modified=true;
    }
  }
  this.enableButtons();
}


BBTeam.prototype.enableButtons = function() {
  if (this.data.team.team.can_have_apo=='Y') {
    if ($(this.objectName+'apothecary').value>=1 || $(this.objectName+'money').value<50) $('apop').disabled=true; else $('apop').disabled=false;
    if ($(this.objectName+'apothecary').value<=0) $('apom').disabled=true; else $('apom').disabled=false;
  }
  if ($(this.objectName+'coaches').value>=99 || $(this.objectName+'money').value<10) $('coap').disabled=true; else $('coap').disabled=false;
  if ($(this.objectName+'coaches').value<=0) $('coam').disabled=true; else $('coam').disabled=false;
  if ($(this.objectName+'cheerleaders').value>=99 || $(this.objectName+'money').value<10) $('chep').disabled=true; else $('chep').disabled=false;
  if ($(this.objectName+'cheerleaders').value<=0) $('chem').disabled=true; else $('chem').disabled=false;
  if (this.noGames) {
    if ($(this.objectName+'rerolls').value>=8 || $(this.objectName+'money').value<parseInt(this.data.team.team.reroll_cost)) $('rerp').disabled=true; else $('rerp').disabled=false;
  } else {
    if ($(this.objectName+'rerolls').value>=8 || $(this.objectName+'money').value<2*parseInt(this.data.team.team.reroll_cost)) $('rerp').disabled=true; else $('rerp').disabled=false;
  }
  if ($(this.objectName+'rerolls').value<=0) $('rerm').disabled=true; else $('rerm').disabled=false;
  if (this.noGames) {
    if ($(this.objectName+'fanfactor').value>=9 || $(this.objectName+'money').value<10) $('fanp').disabled=true; else $('fanp').disabled=false;
    if ($(this.objectName+'fanfactor').value<=0) $('fanm').disabled=true; else $('fanm').disabled=false;
  }
}

BBTeam.prototype.undoTeam = function() {
  this.modified=false;
  this.refreshControl();
}

BBTeam.prototype.saveTeam = function() {
  this.saveTeamName();
  var money=$(this.objectName+'money').value-this.data.team.team.money;
  var apothecary=$(this.objectName+'apothecary').value-this.data.team.team.apothecary;
  var cheerleaders=$(this.objectName+'cheerleaders').value-this.data.team.team.cheerleaders;
  var coaches=$(this.objectName+'coaches').value-this.data.team.team.coaches;
  var rerolls=$(this.objectName+'rerolls').value-this.data.team.team.rerolls;
  if (money==0 && apothecary==0 && cheerleaders==0 && coaches==0 && rerolls==0) {this.refreshControl(); return};
  desc="Changes to team: "+apothecary+" apo; "+coaches+" ass; "+cheerleaders+" che; "+rerolls+" rr; "+money+" money";

  var xml = "<row><action>insert</action><table>team_mutation</table>";
  xml+="<tea_id_team>"+escape(this.idTeam)+"</tea_id_team>";
  xml+="<m_money>"+money+"</m_money>";
  xml+="<m_apothecary>"+apothecary+"</m_apothecary>";
  xml+="<m_coaches>"+coaches+"</m_coaches>";
  xml+="<m_cheerleaders>"+cheerleaders+"</m_cheerleaders>";
  xml+="<m_rerolls>"+rerolls+"</m_rerolls>";
  xml+="<gam_id_game>"+this.data.team.team.last_game+"</gam_id_game>";
  xml+="<owner>"+user.idUser+"</owner>";
  xml+="<description>"+escape(desc)+"</description>";
  xml += "</row>";
  var r="if ("+this.objectName+".xhReqPopup.xhReq.responseText.indexOf('OK')>0) alert("+this.objectName+".xhReqPopup.xhReq.responseText); alert('"+bbLanguage.updated+"'); "+this.objectName+".recalculate();";
  this.xhReqPopup.post("services/one_insert.php", r, xml);
  this.modified=false;
}


BBTeam.prototype.recalculate = function() {
  this.xhReqPopup.sGet("services/bb_recalculate_team.php?i="+this.idTeam);
  this.refreshControl();
}

BBTeam.prototype.saveTeamName = function() {
  var tn=escape($(this.objectName+'teamname').value);
  var tc=escape($(this.objectName+'idcoach').value);
  var ta=escape($(this.objectName+'teamActive').value);
  if (tn!=this.data.team.team.name || tc!=this.data.team.team.coach || ta!=this.data.team.team.active) {
    this.xhReqPopup.sGet("services/bb_change_team.php?i="+this.idTeam+"&n="+tn+"&c="+tc+"&a="+ta);
  }
//  this.refreshControl();
}

BBTeam.prototype.saveNewTeam = function() {
  this.saveTeamName();
  var xml = "<row><action>update</action><table>team</table>";
  xml+="<id_team>"+escape(this.idTeam)+"</id_team>";
  xml+="<name>"+$(this.objectName+'teamname').value+"</name>";
  xml+="<s_money>"+$(this.objectName+'money').value+"</s_money>";
  if ($(this.objectName+'apothecary')) xml+="<s_apothecary>"+$(this.objectName+'apothecary').value+"</s_apothecary>";
  xml+="<s_coaches>"+$(this.objectName+'coaches').value+"</s_coaches>";
  xml+="<s_cheerleaders>"+$(this.objectName+'cheerleaders').value+"</s_cheerleaders>";
  xml+="<s_rerolls>"+$(this.objectName+'rerolls').value+"</s_rerolls>";
  xml+="<s_fan_factor>"+$(this.objectName+'fanfactor').value+"</s_fan_factor>";
  xml+="<recalculate>Y</recalculate>";
  xml += "</row>";

  var r="if ("+this.objectName+".xhReqPopup.xhReq.responseText.indexOf('OK')>0) alert("+this.objectName+".xhReqPopup.xhReq.responseText); alert('"+bbLanguage.updated+"'); "+this.objectName+".refreshControl();";
  this.xhReqPopup.post("services/one_update.php", r, xml);
  this.modified=false;
}

BBTeam.prototype.retirePlayer = function(id) {
  var xml = "<row><action>insert</action><table>team_mutation</table>";
  xml+="<gam_id_game>"+this.data.team.team.last_game+"</gam_id_game>";
  xml+="<tea_id_team>"+escape(this.idTeam)+"</tea_id_team>";
  xml+="<description>Fired player</description>";
  xml+="<old_player>"+id+"</old_player>";
  xml+="<m_coaches>0</m_coaches>";
  xml+="<owner>"+user.idUser+"</owner>";
  xml += "</row>";
  var r="if ("+this.objectName+".xhReqPopup.xhReq.responseText.indexOf('OK')<0) alert("+this.objectName+".xhReqPopup.xhReq.responseText); "+this.objectName+".recalculate();";
  this.xhReqPopup.post("services/one_insert.php", r, xml);
}

BBTeam.prototype.canAdd = function(position) {
  if (parseInt(position.cost)>$(this.objectName+'money').value) return '';
  return "<option value='"+position.id_position+"'>"+unescape(position.position)+"</option>";
}

BBTeam.prototype.addPlayer = function() {
  if (this.modified) {
    if (confirm(bbLanguage.teamChanged)) this.saveNewTeam();
  }

  //create a popup
  var buttons="";
  buttons += "<input type='button' value='"+gLanguage.buttonCancel+"' onClick=javascript:closeForm('cancel') />";
  buttons += "<input type='button' value='"+gLanguage.buttonOK+"' onClick=javascript:closeForm('OK') /> ";

  var positions = "";
  if (this.data.team.position) {
    if (this.data.team.position.length>1) {
       for (m=0; m<this.data.team.position.length; m++) positions+=this.canAdd(this.data.team.position[m]);
    } else positions+=this.canAdd(this.data.team.position);
  }

  var popup = "";
  popup += "<div class='bb_span'><h1 class='bb_header'>"+bbLanguage.newPlayerCaption+"</h1><form id='modalform'>"
  popup += "<table>";
  popup +="<tr><td>"+bbLanguage.playerPosition+"</td><td><select id='pos_id_position'>"+positions+"</select></td></tr>";
  popup +="<tr><td>"+bbLanguage.playerNumberLong+"</td><td><input id='number' value='' size='4' /></td></tr>";
  popup +="<tr><td>"+bbLanguage.playerName+"</td><td><input id='name' value='' /></td></tr>";
  popup +="<tr><td>"+bbLanguage.playerGender+"</td><td><select id='gender'><option value='M'>"+bbLanguage.playerMale+"</option><option value='F'>"+bbLanguage.playerFemale+"</option></select></td></tr>";

  popup += "</table></form><div align='center'>"+buttons+"</div></div>";

  ModalDialogShow(bbLanguage.newPlayerCaption, popup, this.objectName+".addPlayerReturnMethod()");
}

BBTeam.prototype.addPlayerReturnMethod = function() {
  var returnValue =  modalDialog.value;
  modalDialogRemoveWatch();
  if (returnValue=='cancel' || returnValue=="" || returnValue==undefined) return;
  var game
  if (this.data.team.team.last_game) game = this.data.team.team.last_game;
  else game='';

  returnValue="<row><tea_id_team>"+this.idTeam+"</tea_id_team><gam_id_game>"+game+"</gam_id_game>"+returnValue+"</row>";

  var r="if ("+this.objectName+".xhReqPopup.xhReq.responseText.indexOf('OK')<0) alert("+this.objectName+".xhReqPopup.xhReq.responseText); "+this.objectName+".refreshControl();";
  this.xhReqPopup.post("services/bb_add_player.php", r, returnValue);
}


BBTeam.prototype.doUndo = function(id) {
  var xml = new JKL.ParseXML("services/bb_undo_team_mutation.php?i="+this.idTeam+"&m="+id);
  this.data = xml.parse();
  if (this.data.error) {
    $(this.HTMLParent).innerHTML="<div class='error'>Error "+this.data.error.code+": "+this.data.error.message+"</div>";
    return;
  }
  this.recalculate();
}

BBTeam.prototype.showOwnTeam = function() {
  var teamname=myunescape(this.data.team.team.name);
  if (teamname=="&nbsp;") teamname=bbLanguage.noName;
  var tmname=myunescape(this.data.team.team.name);
  if (tmname==undefined) teamname='';
  var res="<div class='bb_span'><h1 class='bb_header'>"+teamname+"</h1>";
  res+=this.menu;

  if (this.data.team.team.last_game==undefined) {
    this.noGames=true;
    var money=this.data.team.team.s_money;
    var apothecary=this.data.team.team.s_apothecary;
    var cheerleaders=this.data.team.team.s_cheerleaders;
    var coaches=this.data.team.team.s_coaches;
    var rerolls=this.data.team.team.s_rerolls;
    var fan_factor=this.data.team.team.s_fan_factor;
  } else {
    this.noGames=false;
    var money=this.data.team.team.money;
    var apothecary=this.data.team.team.apothecary;
    var cheerleaders=this.data.team.team.cheerleaders;
    var coaches=this.data.team.team.coaches;
    var rerolls=this.data.team.team.rerolls;
    var fan_factor=this.data.team.team.fan_factor;
  }
  res+="<div class='bb_content'><table>";
  res+="<tr><td>"+bbLanguage.teamTeamname+"</td><td><input id='"+this.objectName+"teamname' value='"+tmname+"' /></td></tr>";
  res+="<tr><td>"+bbLanguage.teamActive+"</td><td><select id='"+this.objectName+"teamActive'>";
  if (this.data.team.team.active=='Y') res+=bbLanguage.optionYesNoY;
  else res+=bbLanguage.optionYesNoN;
  res+="</select></td></tr>";

  var selcoaches=unescape(this.xhReqPopup.sGet("services/bb_coaches_of_user.php?c="+this.data.team.team.coach));
  res+="<tr><td>"+bbLanguage.teamCoach+"</td><td><select id='"+this.objectName+"idcoach'>"+selcoaches+"</select></td></tr>";

  res+="<tr><td>"+bbLanguage.teamRace+"</td><td>"+unescape(this.data.team.team.racename)+"</td></tr>";
  res+="<tr><td>"+bbLanguage.teamMoney+"</td><td><input class='bb_int' id='"+this.objectName+"money' value='"+money+"' size='4' readonly /></td></tr>";
  //no match played
  if (this.data.team.team.can_have_apo=='Y') {
    res+="<tr><td>"+bbLanguage.teamApothecary+"</td><td><input class='bb_int' id='"+this.objectName+"apothecary' value='"+apothecary+"' size='4' readonly />";
    res+="<input id='apom' type='button' value='-' onclick='"+this.objectName+".clickB(1, -1)' /><input id='apop' type='button' value='+' onclick='"+this.objectName+".clickB(1, 1)' /></td></tr>";
  }
  res+="<tr><td>"+bbLanguage.teamCoaches+"</td><td><input class='bb_int' id='"+this.objectName+"coaches' value='"+coaches+"' size='4' readonly />";
  res+="<input id='coam' type='button' value='-' onclick='"+this.objectName+".clickB(2, -1)' /><input id='coap' type='button' value='+' onclick='"+this.objectName+".clickB(2, 1)' /></td></tr>";
  res+="<tr><td>"+bbLanguage.teamCheerleaders+"</td><td><input class='bb_int' id='"+this.objectName+"cheerleaders' value='"+cheerleaders+"' size='4' readonly />";
  res+="<input id='chem' type='button' value='-' onclick='"+this.objectName+".clickB(3, -1)' /><input id='chep' type='button' value='+' onclick='"+this.objectName+".clickB(3, 1)' /></td></tr>";
  res+="<tr><td>"+bbLanguage.teamRerolls+"</td><td><input class='bb_int' id='"+this.objectName+"rerolls' value='"+rerolls+"' size='4' readonly />";
  res+="<input id='rerm' type='button' value='-' onclick='"+this.objectName+".clickB(4, -1)' /><input id='rerp' type='button' value='+' onclick='"+this.objectName+".clickB(4, 1)' /></td></tr>";

  if (this.noGames) {
    res+="<tr><td>"+bbLanguage.teamFanFactor+"</td><td><input class='bb_int' id='"+this.objectName+"fanfactor' value='"+fan_factor+"' size='4' readonly />";
    res+="<input id='fanm' type='button' value='-' onclick='"+this.objectName+".clickB(5, -1)' /><input id='fanp' type='button' value='+' onclick='"+this.objectName+".clickB(5, 1)' /></td></tr>";
    res+="</table>";
    res+="<div class='bb_right'><input type='button' value='"+bbLanguage.saveChanges+"' onclick='"+this.objectName+".saveNewTeam()' /><br />&nbsp;</div>";
  } else {
    res+="<tr><td>"+bbLanguage.teamFanFactor+"</td><td><input class='bb_int' id='"+this.objectName+"fanfactor' value='"+fan_factor+"' size='4' readonly />";
    res+="</table>";
    res+="<div class='bb_right'>";
    res+="<input type='button' value='"+bbLanguage.undoChanges+"' onclick='"+this.objectName+".undoTeam()' />";
    res+="<input type='button' value='"+bbLanguage.saveChanges+"' onclick='"+this.objectName+".saveTeam()' /><br />&nbsp;</div>";
  }
  res+="<h2 class='bb_header'>"+bbLanguage.teamPlayers+"</h2>";
  res+="<div class='bb_right'><input type='button' value='"+bbLanguage.addPlayer+"' onclick='"+this.objectName+".addPlayer()' /><br />&nbsp;</div>";
  res+="<table>";
  if (this.data.team.player) {
    if (this.data.team.player.length>1) {
      for (m=0; m<this.data.team.player.length; m++) {
        res+=bbShowPlayer(this.data.team.player[m], true, this.data.team.team.last_game);
        if (!this.noGames) res+="</table><div align=right><input type='button' value='"+bbLanguage.teamRetire+"' onclick='"+this.objectName+".retirePlayer("+this.data.team.player[m].id_player+")' /><br />&nbsp;</div><table>";
      }
    } else {
      res+=bbShowPlayer(this.data.team.player, true, this.data.team.team.last_game);
      if (!this.noGames) res+="</table><div align=right><input type='button' value='"+bbLanguage.teamRetire+"' onclick='"+this.objectName+".retirePlayer("+this.data.team.player.id_player+")' /><br />&nbsp;</div><table>";
    }
  }
  res+="</table>";

  res+="<h2 class='bb_header'>"+bbLanguage.teamUndo+"</h2>";
  if (this.data.team.undo) {
    res+="<table class='invis'>";
    if (this.data.team.undo.length>1) {
      for (m=0; m<this.data.team.undo.length; m++) {
        res+="<tr><td>"+unescape(this.data.team.undo[m].description)+"</td>";
        res+="<td align='right'><input type='button' value='"+bbLanguage.teamDoUndo+"' onclick='"+this.objectName+".doUndo("+this.data.team.undo[m].id_team_mutation+")' /></td></tr>";
      }
    } else {
      res+="<tr><td>"+unescape(this.data.team.undo.description)+"</td>";
      res+="<td align='right'><input type='button' value='"+bbLanguage.teamDoUndo+"' onclick='"+this.objectName+".doUndo("+this.data.team.undo.id_team_mutation+")' /></td></tr>";
    }
    res+="</table>";
  }
  res+="<br /><div class='bb_right'><input type='button' onclick='javascript:back()' value='"+gLanguage.buttonClose+"' /></div>";

  res+="</div>"+bbFooter()+"</div>";

  return res;
}

BBTeam.prototype.showOtherTeam = function() {
  var teamname=this.data.team.team.name;
  if (teamname==undefined) teamname=bbLanguage.noName;
  var res="";
  res+="<div class='bb_span'><h1 class='bb_header'>"+unescape(teamname)+"</h1>";
  res+=this.menu;
  res+="<div class='bb_content'><table>";
  res+="<tr><td>"+bbLanguage.teamCoach+" <a href='profile.php?i="+this.data.team.team.owner+"'>"+unescape(this.data.team.team.coachname)+"</a></td>";
  res+="<td>"+bbLanguage.teamRace+" "+unescape(this.data.team.team.racename)+"</td>";
  res+="<td class='bb_pv'>"+bbLanguage.teamTeamValue+" "+myunescape(this.data.team.team.team_value)+"</td>";
  res+="</tr>";
  res+="<tr><td class='bb_ps' colspan='3'></td></tr>";
  res+="</table>";

  if (this.data.team.team.journeymen>0) {
    var d="";
    if (this.data.team.team.journeymen>1) d=" journeyman "+makePlural(unescape(this.data.team.team.journeymen_desc));
    else d=" journeyman "+unescape(this.data.team.team.journeymen_desc);
    res+="<table><tr><td class='bb_pn'></td><td>"+this.data.team.team.journeymen+d+"</td><td class='bb_pv'>"+bbLanguage.playerValue+" "+this.data.team.team.journeymen*this.data.team.team.journeyman_value+"</td></tr>";
    res+="<tr><td class='bb_ps' colspan='3'></td></tr></table>";
  }
  res+="<table>";
  if (this.data.team.player) {
    if (this.data.team.player.length>1) {
      for (m=0; m<this.data.team.player.length; m++) res+=bbShowPlayer(this.data.team.player[m], true, this.data.team.team.last_game);
    } else res+=bbShowPlayer(this.data.team.player, true, this.data.team.team.last_game);
  }
  res+="</table>";

  res+="<table>";
  res+="<tr><td>"+bbLanguage.teamRerolls+" "+myunescape(this.data.team.team.rerolls)+"<br />";
  res+=bbLanguage.teamFanFactor+" "+myunescape(this.data.team.team.fan_factor)+"<br />";
  res+=bbLanguage.teamMoney+" "+myunescape(this.data.team.team.money)+"</td>";

  res+="<td>"+myunescape(this.data.team.team.coaches)+" "+bbLanguage.teamCoaches+"<br />";
  res+=myunescape(this.data.team.team.cheerleaders)+" "+bbLanguage.teamCheerleaders+"<br />";
  if (this.data.team.team.apothecary>0) res+=unescape(this.data.team.team.apothecary)+" "+bbLanguage.teamApothecary;
  res+="</td></tr></table>";
  if (user.loggedIn) {
    if (user.isAdmin) res+="<p class='bb_recalc'><a href='javascript:"+this.objectName+".recalculate()'>recalculate</a> <a href='services/bb_recalculate_team.php?s&i="+this.data.team.team.id_team+"'>showRecalculate</a></p>";
    else res+="<p class='bb_recalc'><a href='javascript:"+this.objectName+".recalculate()'>recalculate</a></p>";
  }
  res+="</div>"+bbFooter()+"</div>";

  return res;
}

BBTeam.prototype.showPlayerList = function(player) {
  var res="";
  if (player) {
    if (player.length>1) {
      for (m=0; m<player.length; m++) res+="<a href='player.php?i="+player[m].id_player+"'>"+myunescape(ifEmpty(player[m].name, ifEmpty(player[m].number, player[m].position)))+"</a><br />";
    } else res+="<a href='player.php?i="+player.id_player+"'>"+myunescape(ifEmpty(player.name, ifEmpty(player.number, player.position)))+"</a><br />";
  } else res="&nbsp;";
  return res;
}

BBTeam.prototype.showGame = function(game) {
  var res="";
  res+="<tr><td><a href='game.php?i="+game.id_game+"'>"+myunescape(game.description)+"</a></td><td>"+myunescape(game.h_td)+" - "+myunescape(game.a_td)+"</td><td><a href='team.php?i="+this.idTeam+"&h="+game.id_game+"'>"+bbLanguage.teamOldSheet+"</a></td></tr>";
  return res;
}

BBTeam.prototype.showTeamHistory = function() {
  var res="";
  var xml = new JKL.ParseXML("services/bb_team_history.php?i="+this.idTeam);
  this.data = xml.parse();
  if (this.data.error) {
    $(this.HTMLParent).innerHTML="<div class='error'>Error "+this.data.error.code+": "+this.data.error.message+"</div>";
    return;
  }
  if (this.data.team) {
    if (this.data.team.team) {
      if (this.data.team.team.recalculate=='Y' && this.historicalGame==0) {
        this.xhReqPopup.sGet("services/bb_recalculate_team.php?i="+this.idTeam);
        this.refreshControl();
      }
      res+="<div class='bb_span'><h1 class='bb_header'>"+myunescape(this.data.team.team.name)+"</h1>";
      res+=this.menu;
      res+="<div class='bb_content'>";
      res+="<table><tr class='bb_header'><td width='33%'>"+bbLanguage.teamActivePlayers+"</td><td width='33%'>"+bbLanguage.teamRetiredPlayers+"</td><td width='33%'>"+bbLanguage.teamDeadPlayers+"</td></tr>";
      res+="<tr><td>"+this.showPlayerList(this.data.team.player)+"</td>";
      res+="<td>"+this.showPlayerList(this.data.team.retiredplayer)+"</td>";
      res+="<td>"+this.showPlayerList(this.data.team.deadplayer)+"</td></tr>";
      res+="</table><br />";
      res+="<table><tr class='bb_header'><td colspan='3'>"+bbLanguage.teamGames+"</td></tr>";
      if (this.data.team.game) {
        if (this.data.team.game.length>1) {
          for (m=0; m<this.data.team.game.length; m++) res+=this.showGame(this.data.team.game[m]);
        } else res+=this.showGame(this.data.team.game);
      }
      res+="</table>";

      res+="</div>"+bbFooter()+"</div>";
    }
  }
  return res;
}
