//
//   copyright  ManagerZone AB 2002-2008
//

var plinks;
var hasLambda = 0;
var tr_links;
var tr_bids_str;
var clubMember = 0;
var p_isRetiredPlayersPage;
var p_isShortListPage;

var playerType = 1;

str = "##mz##";
str = str.replace(/##([^#]*)##/gi, function(str, p1, p2) { return p1; } );
if (str.substring(0, 8) == "function") {
  hasLambda = 0;
}
else {
  hasLambda = 1;
}


function SinglePlayerMonitorHtml(i, national)
{
  plinks = "";
  tr_links = "";
  p = theMonitorPlayers[i];
  tr_link = "<img src=\"img/sell.gif\" border=\"0\" style=\"-moz-opacity:0.60; filter:alpha(opacity=60);\">";
  if (p.type == "Buy" && p.my_team_id != p.bidder_team_id) {
    var amount = 0;
    if (p.tr_lastbid == 0)
      amount = p.tr_startbid;
    else
      amount = p.tr_lastbid*1.05;

    amount = exchange(50,
                      p.currency_id,
                      amount,
                      exchange_rates);

    tr_link = "<a target=_parent href=\"javascript: "+ p.buy_icon +"\"><img src=\"img/sell.gif\" width=23 height=21 border=\"0\"></a>";
  }

  eval("template = p_playerMonitor"+ p.type +"Template");

  return doTheReplace(template, p);
}



function getAltTitle(str)
{
	return " alt='" + str + "' title='" + str + "' ";
}

//var 

//this removes the ##....##
function doTheReplace(template, p)
{
  if (hasLambda == 1) {
    template = template.replace(/##([^#]*)##/gi, function(str, p1, p2) { return eval(p1); } );
  }
  else {
    var re = /##([^#]*)##/gi;
    var reArr = re.exec(template);
    var varn;
    var theVal;
    while (reArr) {
      varn = reArr[0];
      theVal = eval(reArr[1]);
      template = template.replace(varn, theVal);
      re = /##([^#]*)##/gi;
      reArr = re.exec(template);
    }
  }
  return template;
}


function monitorShowHide(buysell)
{
  visible = (document.getElementById("monitordiv00").style.display == "block" || document.getElementById("monitordiv10").title == "") ? 0 : 1;
  if (buysell != visible) {
    visible = (document.getElementById("monitordiv"+buysell+"0").title != "") ? buysell : visible;
    //visible = buysell;
  }
  for (var i=0; i<2; i++) {
    for (var j=0; j<5; j++) {
      mdiv = "monitordiv"+i+""+j;
      if (i == visible && document.getElementById(mdiv).title != "")
        document.getElementById(mdiv).style.display = "block";
      else if (document.getElementById(mdiv).title != "No")
        document.getElementById(mdiv).style.display = "none";
    }
  }
}




function renderPlayerHtmlUpdate(national)
{

  // Render monitored players
  if (document.getElementById("monitorDiv")) { //IF label: 1
    visible = (document.getElementById("monitordiv00").style.display == "block") ? 0 : 1;
    var cntMonitor = new Array(0,0);
    for (var i=0; i<theMonitorPlayers.length; i++) {
      pls_monitor = "";
      type = (theMonitorPlayers[i].type == "Buy") ? 0 : 1;
      mdiv = "monitordiv"+type+""+cntMonitor[type];
      if (cntMonitor[type] < 5 && document.getElementById(mdiv)) {
        title_old = document.getElementById(mdiv).title;
        pls_monitor = SinglePlayerMonitorHtml(i, national);
        if (i == visible)
          document.getElementById(mdiv).style.display = "block";
        document.getElementById(mdiv).title = theMonitorPlayers[i].title;
        document.getElementById(mdiv).innerHTML = pls_monitor;
        if (title_old != "" && title_old != theMonitorPlayers[i].title) {
          // Only update when changed content
          str = (type == 0) ? "Buy" : "Sell";
        }
        cntMonitor[type]++;
      }
    }
    var mostof = (cntMonitor[0] > 0 && cntMonitor[0] >= cntMonitor[1]) ? 0 : 1;
    document.getElementById("nrBuy").innerHTML = p_strmonitorBuy +" ("+ cntMonitor[0] +")";
    document.getElementById("nrSell").innerHTML = p_strmonitorSell +" ("+ cntMonitor[1] +")";
    // Reset unused elements
    for (var i=0; i<2; i++) {
      for (var j=cntMonitor[i]; j<5; j++) {
        mdiv = "monitordiv"+i+""+j;
        if (j == -1) {
          document.getElementById(mdiv).innerHTML = "<center><strong>"+strNoPlayers+"</strong></center>";
          document.getElementById(mdiv).style.display = "block";
          document.getElementById(mdiv).title = "No";
        }
        else {
          document.getElementById(mdiv).innerHTML = "";
          document.getElementById(mdiv).style.display = "none";
          document.getElementById(mdiv).title = "";
        }
      }
    }
    document.getElementById("monitorDiv").style.height = 40 + (cntMonitor[mostof] * 43);
    visible = (document.getElementById("monitordiv"+visible+"0").title != "") ? visible : Math.abs(visible-1);
    monitorShowHide(visible);
  } //IF label: 1

  // Render transfer players
  if (playerType == 2 && frames['players_iframe'] ) {
    if (frames['players_iframe'].document.getElementById("errormsg") && error_content == "") {
      frames['players_iframe'].document.getElementById("errormsg").innerHTML = "";
      frames['players_iframe'].document.getElementById("errormsg").style.display = "none";
    }

    for (var i=0; i<20; i++) {
      pdiv = "playerdiv"+i;
      pls = "";
      title = "";
      if (i<thePlayers.length) {
        pls += ""+ thePlayers[i].toHTML(national);
        title = thePlayers[i].title;
      }
      else if (i==0) {
        pls += "<table width='100%'><tr><td align='center'><br><br><br><strong>"+strNoPlayersFound+"</strong></td></tr></table>";
        title = " ";
      }
      if ( title != "" && frames['players_iframe'].document.getElementById(pdiv).title != title ) {
        frames['players_iframe'].document.getElementById(pdiv).innerHTML = pls;
        frames['players_iframe'].document.getElementById(pdiv).title = title;
      }
      if (pls == "")
        frames['players_iframe'].document.getElementById(pdiv).style.display = "none";
      else
        frames['players_iframe'].document.getElementById(pdiv).style.display = "block";
    }
  }

}


function renderPlayerAjaxHtml(national)
{
  if ( playerType > 0 ) {
    if ( frames['players_iframe'] ) {
      if ( playerType == 2 && frames['players_iframe'].document.getElementsByTagName('div').length < 20 ) {
        renderPlayerHtml(national);
      }
      else {
        renderPlayerHtmlUpdate(national);
      }
    }
    else
      renderPlayerHtmlUpdate(national);
  }
}

function clearTransferSearch()
{
  if ( playerType > 1 && frames['players_iframe'] ) {
    ply = "<table width='100%'><tr><td align='center'><br><br><br><strong>"+strWaitForPlayers+"...</strong></td></tr></table>";
    iframe_write("players", ply);
  }
}




function renderMonitorPlayerHtml()
{
  if (document.getElementById('monitorDiv') && theMonitorPlayers != undefined) {
    pls2 = "";
    for (j=0; j<2; j++) {
      mdivmain = "monitorMain"+j;
      pls2 += "<div id=\""+ mdivmain +"\" name=\""+ mdivmain +"\">";
      pls2 += "<table width='100%' cellspacing=0 cellpadding=0 border=0>";
      str = (j == 0) ? "Buy" : "Sell";
      pls2 += "<tr class=\"seriesHeader\"><td align='center' class=\"subheader\" onClick=\"monitorShowHide("+j+");\" style=\"cursor: pointer;\">";
      pls2 += "<strong><div id=\"nr"+str+"\" name=\"nr"+str+"\">"+ eval("p_strmonitor"+str) +"</div></strong></td></tr>";
      pls2 += "<tr><td height=1 bgcolor=black></td></tr><tr><td valign=\"top\">";
      pls = "";
      for (var i=0; i<5; i++) {
        mdiv = "monitordiv"+j+""+i;
        ply = "";
        if (i==0) {
          ply = "<table width='100%'><tr><td align='center'><i>"+strWaitForPlayers+"...</i></td></tr></table>";
          pls += "<div id=\""+ mdiv +"\" name=\""+ mdiv +"\" title=\"\" style=\"display:block;\">"+ ply +"</div>";
        }
        else
          pls += "<div id=\""+ mdiv +"\" name=\""+ mdiv +"\" title=\"\" style=\"display:none;\">"+ ply +"</div>";
      }
      pls2 += pls+"</td></tr></table></div";
    }
    layer_write("monitorDiv", pls2);
    // Set default to show bayig players
    monitorShowHide(0);
  }
}

function onIframeScroll( iframeref )
{
	var scrolly = getScrollY(iframeref);
	var winheight = getWindowSize(iframeref);
	var safetymargin = 200;
	for ( var i = lastRenderedPlayer; i < thePlayers.length; ++i ) {
		if ( renderedPlayerIndexes[thePlayers[i].player_id] != 1 ) {
			var elm = iframeref.document.getElementById('thePlayers_' + i);
			if ( elm && findPos(elm).y - scrolly <= winheight + safetymargin ) {
				replacePlayerPlaceholder( iframeref, i );
				lastRenderedPlayer = i;
			}
		}
	}
}

function findPos( obj ) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft;
    curtop = obj.offsetTop;
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
  }
  return {x:curleft, y:curtop};
}
function getScrollY(win) {
	if ( win.document.documentElement && win.document.documentElement.scrollTop )
		return win.document.documentElement.scrollTop;
	if ( win.document.body && win.document.body.scrollTop )
		return win.document.body.scrollTop;
	return 0;
}
function getWindowSize(win) {
	if ( document.body.clientHeight )
		return document.body.clientHeight;
	if ( document.innerHeight )
		return document.innerHeight;
	return 0;
}


function replacePlayerPlaceholder( iframeref, index )
{
	lastRenderedPlayer = index;
	
	renderedPlayerIndexes[thePlayers[index].player_id] = 1;
	var newdiv = iframeref.document.createElement("div");
	newdiv.innerHTML = thePlayers[index].toHTML(lastnational);
	
	iframeref.document.getElementById('content').replaceChild(newdiv, iframeref.document.getElementById("thePlayers_"+index));
	newdiv.id = "thePlayers_"+index;
}

var renderedPlayerIndexes = new Array();
var lastRenderedPlayer = 0;
var lastnational = null;
function renderPlayerHtml(national)
{
	lastnational = national;
	var renderLength = isSafari() ? thePlayers.length : -1;

  if (playerType == 1 && frames['players_iframe'] && thePlayers != undefined) {
    var endPlayers = getWindowSize(frames["players_iframe"]);

    var pls = "";
    var sumHeight = 0;
    for (var i=0; i<thePlayers.length; i++) {
      pls += "<div id=thePlayers_"+i+">"+thePlayers[i].toHTML(national)+"</div>";
      renderedPlayerIndexes[thePlayers[i].player_id] = 1;
      lastRenderedPlayer = i;
      sumHeight += 280; //aproximate size of a player section
      if ( sumHeight > getWindowSize(frames['players_iframe']) && renderLength == -1 ) {
        ++i;
        for ( ; i < thePlayers.length; ++i ) {
          pls += "<div id=thePlayers_"+i+" style='height: 280px;'></div>";
        }
        break;
      }
    }
    iframe_write("players", pls);
  }
  else if ( playerType > 1 && frames['players_iframe'] ) {
    if ( frames['players_iframe'].document.getElementsByTagName('div').length < 20 ) {
      pls = "";
      if ( typeof(error_content) != "undefined" && error_content != "") {
        pls += "<div id=\"errormsg\" name=\"errormsg\">"+ error_content +"</div>";
      }
      for (var i=0; i<20; i++) {
        pdiv = "playerdiv"+i;
        ply = "";
        if (i==0)
          ply = "<table width='100%'><tr><td align='center'><br><br><br><strong>"+strWaitForPlayers+"...</strong></td></tr></table>";
        pls += "<div id=\""+ pdiv +"\" name=\""+ pdiv +"\">"+ ply +"</div>";
      }

      iframe_write("players", pls);

      if (playerType == 2) 
        setTimeout("renderPlayerHtmlUpdate("+national+");",1500);
      else
        setTimeout("renderPlayerHtml("+national+");",5000);
    }
    else {
      renderPlayerHtmlUpdate(national);
    }

  }
  var end = new Date();
}

var scr_step;
var scr_obj;
var scr_cur_y = 0;
var scr_end_y;
var scr_time_per_step = 10;
var scr_steps;
var bookmarkId = "thePlayers_0";

function doScroll(step)
{
  scr_delta = scr_end_y - scr_cur_y;
  scr_step = Math.ceil(scr_delta / (scr_steps/24));
  scr_cur_y += scr_step;

  scr_obj.scrollTo(0, scr_cur_y);
  if ( Math.abs(scr_cur_y - scr_end_y) > 3 && scr_steps >= step ) {
    window.setTimeout("doScroll(" + (step + 1) + ")", scr_time_per_step);
  }
  else {
    window.setTimeout("doScrollCheck()");
  }
}

function doScrollCheck() {
  if ( !scr_obj ) {
    return;
  }
  var real_y = findPos(scr_obj.document.getElementById(bookmarkId)).y;
  if ( Math.abs(scr_cur_y - real_y) >= 3 ) {	//3px wrong should be ok to diff from transition
	  scr_obj.scrollTo(0, real_y );
	  scr_obj = null;
  } 
}

function startScroll(obj, to_y, time)
{
  var cur_y = 0;
  scr_end_y = to_y;
  scr_steps = (time/scr_time_per_step);
  scr_obj = obj;
  window.setTimeout("doScroll(1)", scr_time_per_step);
}

function scrollToPlayer(iframe, ply_idx)
{
  bookmarkId = 'thePlayers_'+ply_idx;
  var gip = document.getElementById(iframe);
  y = gip.contentWindow.document.getElementById(bookmarkId).offsetTop;
  startScroll(frames[iframe], y, 600);
}

function Player(player_id,
                team_id,
                national_team_id,
                national_shirt_no,
                national_shirtpricolor,
                national_shirtseccolor,
                national_shirtpattern,
                nationalU21_team_id,
                nationalU21_shirt_no,
                nationalU21_shirtpricolor,
                nationalU21_shirtseccolor,
                nationalU21_shirtpattern,
                team_name,
                player_name,
                t_salary,
                retiring,
                retiring_date,
                tc_cur_season,
                status,
                origin_country_id,
                origin_country_shortname,
                origin_country_name,
                origin_country_translated_name,
                skills,
                flags,
                tr_bids,
                attributes,
                stats,
                shirtnr,
                show_on_tactics,
                value,
                salary,
                age,
                ontransfer,
                sell,
                transferarray,
                shirtpricolor,
                shirtseccolor,
                shirtpattern,
                junior, 
                shortlist,
                currency_suffix,
                currency_id,
                mz_currency_id,
                injury,
                training_camp,
                warning)
{

  if ( typeof(player_id) == "undefined" )	//fix to remove the overload errors while parsing the subclasses and nothing is passed to the constructor
    return;	

  this.player_id = player_id;
  this.team_id = team_id;
  this.team_name = team_name;
  this.player_name = player_name;

  this.strsalary = p_strsalary;
  this.digitalGoodsArray = null;

  this.t_salary = t_salary;
  this.retiring = retiring;
  this.retiring_date = retiring_date;
  this.tc_cur_season = tc_cur_season;
  this.status = status;
  this.origin_country_id = origin_country_id;
  this.origin_country_shortname = typeof(origin_country_shortname) != "undefined" ? origin_country_shortname.toLowerCase() : "";
  this.origin_country_name = origin_country_name;
  this.origin_country_translated_name = origin_country_translated_name;
  this.skills = skills;
  this.flags = flags;
  this.tr_bids = tr_bids;
  this.attributes = attributes;
  this.stats = stats;
  this.shirtnr = shirtnr;
  this.show_on_tactics = show_on_tactics;  //Deprecated?
  this.value = value;
  this.salary = salary;
  this.age = age;
  this.junior = junior;
  this.juniorstr = this.junior ? p_strjunior : "";
  this.ontransfer = ontransfer;
  this.sell = sell;
  this.currency_suffix = currency_suffix;
  this.currency_id = currency_id;
  this.mz_currency_id = mz_currency_id;

  if (status == "employed")
    this.appearanceButton = typeof(p_appearanceButton) != "undefined" ? p_appearanceButton : "";
  else
    this.appearanceButton = "";

  this.shortlist = shortlist;
  if (this.ontransfer) {
    this.value  = exchange(this.mz_currency_id,
                           this.currency_id,
                           this.value,
                           exchange_rates);
    this.salary = exchange(this.mz_currency_id,
                           this.currency_id,
                           this.salary,
                           exchange_rates);
  }

  this.value_display = presentMoney(this.value);
  this.salary_display = presentMoney(this.salary);

  this.exchanged_value = exchange(this.currency_id,
                                  user_currency_id,
                                  this.value,
                                  exchange_rates);
  this.exchanged_value_display = presentMoney(this.exchanged_value);

  this.exchanged_salary = exchange(this.currency_id, 
                                   user_currency_id,
                                   this.salary,
                                   exchange_rates);
  this.exchanged_salary_display = presentMoney(this.exchanged_salary);

  this.tr_value =  exchange(this.mz_currency_id,
                            this.currency_id,
                            value,
                            exchange_rates);
  this.tr_value_display = presentMoney(this.tr_value);

  this.tr_salary =  exchange(this.mz_currency_id,
                            this.currency_id,
                            salary,
                            exchange_rates);
  this.tr_salary_display = presentMoney(this.tr_salary);



  this.tr_exchanged_value = exchange(this.currency_id,
                                  user_currency_id,
                                  this.tr_value,
                                  exchange_rates);
  this.tr_exchanged_value_display = presentMoney(this.tr_exchanged_value);

  this.tr_exchanged_salary = exchange(this.currency_id,
                                   user_currency_id,
                                   this.tr_salary,
                                   exchange_rates);
  this.tr_exchanged_salary_display = presentMoney(this.tr_exchanged_salary);

  this.national_team_id = national_team_id;
  this.national_shirt_no = national_shirt_no;
  this.national_shirtpricolor = national_shirtpricolor;
  this.national_shirtseccolor = national_shirtseccolor;
  this.national_shirtpattern = national_shirtpattern;

  this.nationalU21_team_id = nationalU21_team_id;
  this.nationalU21_shirt_no = nationalU21_shirt_no;
  this.nationalU21_shirtpricolor = nationalU21_shirtpricolor;
  this.nationalU21_shirtseccolor = nationalU21_shirtseccolor;
  this.nationalU21_shirtpattern = nationalU21_shirtpattern;

  this.overbid = '';
  if (transferarray && transferarray.length != 0) {
    this.tr_startbid = transferarray[0];

    this.tr_exchanged_startbid_display = presentMoney(exchange(this.mz_currency_id,
                                                               user_currency_id,
                                                               this.tr_startbid,
                                                               exchange_rates));

    this.tr_lastbid = transferarray[1];
    this.tr_exchanged_lastbid = exchange(this.mz_currency_id,
                                         user_currency_id,
                                         this.tr_lastbid,
                                         exchange_rates);
    this.tr_exchanged_lastbid_display = presentMoney(this.tr_exchanged_lastbid);

    this.tr_enddate = transferarray[2];
    this.bidderlink = transferarray[3];
    if (transferarray[4].length != 0)
      this.tr_bidder = '('+transferarray[4]+')';
    else
      this.tr_bidder = '';
    this.tr_biddername = transferarray[5];

    if (this.tr_exchanged_lastbid >= (this.tr_exchanged_value*5))
      this.overbid = '<img src="img/exclamation_mark.gif" width=4 height=11>';
    this.tr_startbid_display = presentMoney(exchange(this.mz_currency_id,
                                                    this.currency_id,
                                                    this.tr_startbid,
                                                    exchange_rates));
    this.tr_lastbid_display = presentMoney(exchange(this.mz_currency_id,
                                                    this.currency_id,
                                                    this.tr_lastbid,
                                                    exchange_rates));
  }
  if (this.attributes[2] == 'r') {
    this.strside = p_str_right;
  }
  else if (this.attributes[2] == 'l') {
    this.strside = p_str_left;
  }
  else {
    this.strside = p_str_both;
  }
  this.strside = p_strsidekey + ": <span class='bold'>" + this.strside + "</span>";
  if ( p_steamid == this.team_id && status == 'employed') {
	  if (this.attributes[7] == '+') {
	    this.formpicture = "<img src='img/formplus.gif' title='"+str_form_rest+"' alt='"+str_form_rest+"'>";
	    this.formtext = str_form_rest;
	  }
	  else if (this.attributes[7] == '-') {
	    this.formpicture = "<img src='img/formminus.gif' title='"+str_form_play+"' alt='"+str_form_play+"'>";
	    this.formtext = str_form_play;
	  }
	  else {
	    this.formpicture = "<img src='img/formgood.gif' title='"+str_form_satisfied+"' alt='"+str_form_satisfied+"'>";
	    this.formtext = str_form_satisfied;
	  }
  }
  else{
    this.formpicture = "";
    this.formtext = "";
  }

  this.shirtpricolor = shirtpricolor;
  this.shirtseccolor = shirtseccolor;
  this.shirtpattern = shirtpattern;
  if (status == 'employed') {
    if (injury && injury.length != 0) {

      this.injury_severity = injury[0];
      this.injury_name = injury[1];
      this.injury_days = injury[2];
      this.injury_translated_name = injury[3];
      this.injury_translated_severity = injury[4];
      this.injuryLink = injury[5];
      this.injuryIcon = '<img src="img/injuries/injury_icon.gif" border=0>';
      if (this.ontransfer) {
        this.injuryTable = '<td align=right><span title="'+str_injury +': '+this.injury_translated_name+' - '+this.injury_translated_severity+'">'+this.injuryIcon+'</span></td>';

      }
      else {
        this.injuryTable = '<tr><td colspan="5">';
        this.injuryTable += '<table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>';
        this.injuryTable += '<td width=1 valign=top class='+p_windowbg+' colspan="5"><img src="img/trans.gif" width="1" height="1" border=0></td>';
        this.injuryTable += '</tr><tr>';
        this.injuryTable += '<td colspan=4 width=1 height=6 valign=top><img src="img/trans.gif" width="1" height="6" border=0></td>';
        this.injuryTable += '</tr><tr>';
        this.injuryTable += '<td width="6"></td>';
        this.injuryTable += '<td align="center" colspan="2"><a target=_parent href="'+this.injuryLink+'">'+this.injuryIcon+'</a></td></tr><tr><td colspan="3">'+this.injury_translated_severity+' '+str_injury+': '+this.injury_translated_name+'</td>';
        this.injuryTable += '</tr><tr>';
        this.injuryTable += '<td colspan=4 width=1 height=6 valign=top><img src="img/trans.gif" width="1" height="6" border=0></td>';
        this.injuryTable += '</tr>';
        this.injuryTable += '</table></td></tr>';
      }
    }
    else {
      this.injuryTable = '';
      this.injuryIcon = '<img src="img/injuries/injury_icon_bw.gif" border=0 style="-moz-opacity:0.40; filter:alpha(opacity=40); opacity:0.4;" />';
    }
  }
  else {
    this.injuryIcon = "";
    this.injuryTable = "";
  }

  if (status == 'employed') { //only show icons for employed players.
    if (this.tc_cur_season == 'y') {
      this.tcThisSeasonInfo = '<td align=right><span title="'+p_strtccurseason+'"><img src="img/icon_trainingcamp.gif" width="15" height="16"></span></td>';
      this.tcThisSeasonInfo = doTheReplace(this.tcThisSeasonInfo, this );
      this.tcThisIcon = '<img src="img/icon_trainingcamp.gif"  />';
    }
    else {
      this.tcThisSeasonInfo = '';
      this.tcThisIcon = '<img src="img/icon_trainingcamp_bw.gif" style="-moz-opacity:0.40; filter:alpha(opacity=40); opacity:0.4;" />';
    }

    if (training_camp) {
      this.tc_name = training_camp[1];
      this.tc_img_name = this.tc_name.toLowerCase();
      this.tc_level = training_camp[0];
      this.tc_type = training_camp[2];
      var imgLink = "img/trainingcamp/tc_"+this.tc_type+"_"+this.tc_level+".gif";
      this.tcIcon = '<img src="'+imgLink+'" border=0>';

      this.tcTable = '<tr><td colspan="5">';
      this.tcTable += '<table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>';
      this.tcTable += '<td width=1 valign=top class='+p_windowbg+' colspan="5"><img src="img/trans.gif" width="1" height="1" border=0></td>';
      this.tcTable += '</tr><tr>';
      this.tcTable += '<td colspan=4 width=1 height=6 valign=top><img src="img/trans.gif" width="1" height="6" border=0></td>';
      this.tcTable += '</tr><tr>';
      this.tcTable += '<td align="left" colspan="3">'+this.tcIcon+'</td></tr><tr><td colspan="3">'+this.tc_name+'</td>';
      this.tcTable += '</tr><tr>';
      this.tcTable += '<td colspan=4 width=1 height=6 valign=top><img src="img/trans.gif" width="1" height="6" border=0></td>';
      this.tcTable += '</tr>';
      this.tcTable += '</table></td></tr>';
    }
    else
      this.tcTable = '';
  }
  else {
    this.tcThisIcon = ''; //no image for fired and retired player.
    this.tcThisSeasonInfo = '';
    this.tcTable = '';
  }

  for (skill in this.skills) {
    if (this.flags[skill] > 0)
      this.flags[skill] = '<img src="img/flag.gif" width="9" height="10" border=0>';
    else
      this.flags[skill] = '';
  }

  this.title = this.player_name +" ("+ this.player_id +") "+ this.tr_exchanged_lastbid_display +" @ "+ this.tr_enddate;

  this.warning = warning;

  var flashSport = 'football';
  if (sport == 'hockey') {
    flashSport = 'hockey';
  }
	this.shirtflash = getFlashString('jersey_viewer', 'swf/' + p_garmentFlashFilename, p_garmentFlashSizeWidth, p_garmentFlashSizeHeight, 'transparent',
                                   'pos_x=-8&pos_y=-5&browser=other&game_mode=' + flashSport + '&shirt_only=' + p_garmentFlashShirtOnly + '&shirt_number=' + this.shirtnr + '&scale=0.5&'+
                                   'shirt1=' + this.shirtpricolor + '&shirt2=' + this.shirtseccolor + '&pattern_type=' + (this.shirtpattern - 1),
                                   'd27cdb6e-ae6d-11cf-96b8-444553540000',
                                   'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0', "swf/", '#F2F1EC');

}
Player.prototype.fetchNationalLinks = function(national)
{
  var p = this;
  var plink = new Array();
  if (national == 1) { //Remove senior player
    plink[plink.length] = '<a target=_parent href="?p=national_teams_admin&sub=players&pid=' + p.player_id + '&extra=remove">'+strRemove+'</a>';
  }
  else if (national == 2 && p.status == "employed") {//Adding Senior player
    plink[plink.length] = '<a target=_parent href="?p=national_teams_admin&sub=players&pid=' + p.player_id + '&extra=add">'+strAdd+'</a>';
  }
  else if (national == 3 && p.status == "employed") { //Remove U-21 player or copy it to senior team
    plink[plink.length] = '<a target=_parent href="?p=national_teams_admin&type=u21&sub=players&pid=' + p.player_id + '&extra=remove">'+strRemoveU21+'</a>';
    plink[plink.length] = '<a target=_parent href="?p=national_teams_admin&sub=players&pid=' + p.player_id + '&extra=add">'+strToSeniors+'</a>';
  }
  else if (national == 4 && p.status == "employed") {//Adding U-21 player
    plink[plink.length] = '<a target=_parent href="?p=national_teams_admin&type=u21&sub=players&pid=' + p.player_id + '&extra=add">'+strAddU21+'</a>';
  }
  else if (national == 5 && p.status == "employed") { //Adding both senior and U-21 player
    plink[plink.length] = '<a target=_parent href="?p=national_teams_admin&sub=players&pid=' + p.player_id + '&extra=add">'+strAdd+'</a>';
    plink[plink.length] = '<a target=_parent href="?p=national_teams_admin&type=u21&sub=players&pid=' + p.player_id + '&extra=add">'+strAddU21+'</a>';
  }
  else if (p.team_id == p_steamid && p.status == "employed") {
    if (p.junior == 0) {
      if (p.ontransfer) {
        plink[plink.length] = '';
        plink[plink.length] = '';
      } 
      else {
        plink[plink.length] = '<a target=_parent href="?p=players&pid=' + p.player_id + '&extra=sell"><img src=\"img/sell.gif\" border=\"0\" ' +getAltTitle(p_strsell)+ ' ><br>' + p_strsell + '</a>';
        plink[plink.length] = '<a target=_parent href="?p=players&pid=' + p.player_id + '&extra=fire"><img src=\"img/discard.gif\" border=\"0\" ' +getAltTitle(p_strfire)+ ' ><br>' + p_strfire + '</a>';
        if (clubMember)
          plink[plink.length] = '<a target=_parent href="?p=players&sub=notes&pid=' + p.player_id + '"><img src=\"img/notes.gif\" border=\"0\" ' +getAltTitle(p_strnotes)+ ' ><br>' + p_strnotes + '</a>';
      }
    }
    else {
      plink[plink.length] = p_strjunior;
      plink[plink.length] = "";
      if (clubMember) {
        plink[plink.length] = '<a target=_parent href="?p=players&sub=notes&pid=' + p.player_id + '"><img src=\"img/notes.gif\" border=\"0\" ' +getAltTitle(p_strnotes)+ ' ><br>' + p_strnotes + '</a>';
      }

    }
  }
  else {
    plink[plink.length] = '';
    plink[plink.length] = '';
    plink[plink.length] = '';

  }
  return plink;
}
Player.prototype.toHTML = function(national)
{
  plinks = "";
  tr_links = "";
  tr_bids_str = "";
  var p = this;
  
  var tr_link = new Array();
  var plink = new Array();

  var nationalLinks = this.fetchNationalLinks(national);
  for ( var i = 0; i < nationalLinks.length; ++i ) {
    plink.push( nationalLinks[i] );
  }

  if (p.ontransfer && p.team_id != p_steamid) {
    var amount = 0;
    if (p.tr_lastbid == 0) {
      amount = p.tr_startbid;
      amount = exchange(p.mz_currency_id,
                        p.currency_id,
                        amount,
                        exchange_rates);
    }
    else {
      amount = Math.ceil(p.tr_lastbid*1.05);
      amount = exchangeCeil(p.mz_currency_id,
                            p.currency_id,
                            amount,
                            exchange_rates);
    }      
    /* We have to replace quotes within the name to avoid Javascript crashes */
    var temp_str = p.player_name;
    var temp_str2 = temp_str.replace(/\'/g,'`'); //Replacing ' with ^ /g means global => change all instances of single quote
    var JSplayer_name = temp_str2;
    var buylink = "<a target=_parent title=\""+p_strbuyplayer+"\" href=\"javascript: parent.buy(" + amount + ", " + p.value + ", " + p.player_id + ", " + p.currency_id  + ", " + user_currency_id  +  " , '" + p.currency_suffix + "', '" + user_currency_suffix  + "', " + p.value + ", " + p.salary + ", " + p.t_salary + ", " + p.warning + ", '" + JSplayer_name + "');\"><img src=\"img/sell.gif\" border=\"0\">";

    plink[plink.length] = buylink +"<br>"+ p_strbuyplayer +"</a>";
    tr_link[tr_link.length] = buylink +"</a>";

    plink[plink.length] = '<a target=_parent href="?p=players&sub=monitor&pid=' + p.player_id + '&extra=transfer"><img src=\"img/monitor.gif\" border=\"0\"  ' +getAltTitle(p_strmonitor)+ ' ><br>' + p_strmonitor + '</a>';
    tr_link[tr_link.length] = '<a target=_parent title=\"' +getAltTitle(p_strmonitor)+ '\" href="?p=players&sub=monitor&pid=' + p.player_id + '"><img src=\"img/monitor.gif\" border=\"0\" ' +getAltTitle(p_strmonitor)+ '></a>';

  }

  if (p.ontransfer) {
    if (p.tr_bids.length != 0) {
      for (var i=0; i<p.tr_bids.length; i++) {
        bid_value = exchange(p.mz_currency_id,
                             p.tr_bids[i][3],
                             p.tr_bids[i][0],
                             exchange_rates);

        exch_bid_value = exchange(p.mz_currency_id,
                                  user_currency_id,
                                  p.tr_bids[i][0],
                                  exchange_rates);


        tr_bids_str += "<span title=\""+ presentMoney(bid_value) +" "+ p.tr_bids[i][4] +"\">"+presentMoney(exch_bid_value)+" "+ user_currency_suffix +"</span>, "+p.tr_bids[i][2]+"<br>";

      }
    }
    else
      tr_bids_str = "No bids yet";
  }

  if (p_isNationalCoach && !p_isNationalCoachPage && 
      p.origin_country_id == p_user_origin_country_id && p.status == "employed") {
    plink[plink.length] = '<a target=_parent href="?p=national_teams_admin&sub=search&pid=' + p.player_id + '">'+strView+'</a>';
  }
  if (p.status == "employed") {
    if (p.shortlist == 1)
      plink[plink.length] = '<a target=_parent href="?p=shortlist&sub=remove&pid=' + p.player_id + '"><img src=\"img/monitor_remove.gif\" border=\"0\"  ' +getAltTitle(p_strremove)+ ' >' + p_strremove + '</a>';
    else
      plink[plink.length] = '<a target=_parent href="?p=players&sub=monitor&pid=' + p.player_id + '&extra=transfer"><img src=\"img/monitor.gif\" border=\"0\"  ' +getAltTitle(p_strmonitor)+ ' ><br>' + p_strmonitor + '</a>';
    tr_link[tr_link.length] = '<a target=_parent title=\"' +getAltTitle(p_strmonitor)+ '\" href="?p=players&sub=monitor&pid=' + p.player_id + '"><img src=\"img/monitor.gif\" border=\"0\" ' +getAltTitle(p_strmonitor)+ '></a>';
  }
  else if (!p_isRetiredPlayersPage && p_isShortListPage) {
    plink[plink.length] = '<a target=_parent href="?p=shortlist&sub=remove&pid=' + p.player_id + '"><img src=\"img/monitor_remove.gif\" border=\"0\"  ' +getAltTitle(p_strremove)+ ' >' + p_strremove + '</a>';  
    //We don't want to display the icon to remove a player if we are on Retired Player Page, but we want to if we are at Monitoring Page
  }
  // Shirts. If nationalteampage and player is nationalTeamPlayer the shirt 
  // should be cached on national_team_id. Otherwise on team_id.
  if ((p_isNationalCoachPage || p_isNationalTeamPage) && 
      p.national_team_id != 0) {
    p.shirtcache_team_id = p.national_team_id;
  }
  else {
    p.shirtcache_team_id = p.team_id;
  }

  if (p.junior == 2) { //trainer
    plink = new Array();
    if (p.team_id == p_steamid)
      plink[plink.length] = '<a target=_parent href="?p=trainers&cid=' + p.player_id + '&extra=fire"><img src=\"img/discard.gif\" border=\"0\" ' +getAltTitle(p_strfire)+ ' >' + p_strfire + '</a>';
  }
  var template = p_playerTemplate;


  if (isSpecialPlayer(p.team_id)) {
    plink = new Array();
  }
  
  for (var i=0; i<plink.length; i++) {
    plinks += p_breaksBeforeLinks + plink[i];
  }

  for (var i=0; i<tr_link.length; i++) {
    tr_links += "&nbsp;" + tr_link[i];
  }

  return doTheReplace(template, this );
}
Player.prototype.generatePlayerSkillsHtml = function()
{
  var skills = "";
  if (p_isNationalCoachPage || 
     this.team_id == p_steamid ||
     this.ontransfer == 1 || 
     isSpecialPlayer(this.team_id)) {
    skills = doTheReplace(p_playerSkillsTemplate, this );
  }
  return skills;
}
Player.prototype.generateRetireInfo = function()
{
  if ( this.retiring ) {
    if ( this.ontransfer )
      return '<td colspan=2 style="padding-left: 5px; padding-right: 5px;"><table width="100%"><tr>' +
        '<td style="background-color: red; height: 25px; color: #FFFFFF;" align=center><b>'+str_retiring +' ('+ this.retiring_date + ')</b></td></tr></table></td>';
    else
      return "<tr><td width=1 height=6 valign=top><img src=\"img/trans.gif\" width=1 height=6 border=0></td></tr>" +
        "<tr><td width=1 valign=top class=" + p_windowbg + "><img src=\"img/trans.gif\" width=1 height=1 border=0></td></tr>" +
        "<tr><td width=1 height=6 valign=top><img src=\"img/trans.gif\" width=1 height=6 border=0></td></tr>" +
        "<tr><td style=\"background-color: red; height: 25px; color: #FFFFFF;\" align=center>&nbsp;<b>" + str_retiring +" ("+ this.retiring_date + ")</b></td></tr>";
  }
  return "";
}
Player.prototype.generateTeamInfoHtml = function()
{
  var p = this;
  teamInfo = "";
  // Show if player is in national team and if on national team or 
  // national coach page
  if ( p && (p.national_team_id != 0 || p.national_teamU21_id != 0)&& 
       (p_isNationalTeamPage || p_isNationalCoachPage)) {
    teamInfo = doTheReplace(p_teamInfoTemplate, this );
  }
  return teamInfo;
}

Player.prototype.generateTransferHtml = function() {
  var p = this;
  transfer = "";
  if ( p && p.ontransfer == 1) {
    transfer = doTheReplace(p_transferTemplate, this );
  }
  return transfer;
}

Player.prototype.generateNationalInfoHtml = function() 
{
  var p = this;
  var html = "";
  if (p.national_team_id > 0 && !p_isNationalTeamPage) {
    html = doTheReplace(p_nationalTeamInfoTemplate, p);
  }
  else if (p.nationalU21_team_id > 0 && !p_isNationalTeamPage) {
    html = doTheReplace(p_nationalU21TeamInfoTemplate, p );
  }
  return html;
}
Player.prototype.renderSinglePlayer = function()
{
  layer_write("playercontent", this.toHTML() );
}
Player.prototype.setDigitalGoods = function(items)
{
	this.digitalGoodsArray = items;
}
Player.prototype.setDGColors = function(skin, hair, facial)
{
	//nothing
}
Player.prototype.addAppearanceButton = function()
{
	return "";
}


function soccerPlayer(player_id,team_id,national_team_id,national_shirt_no,national_shirtpricolor,national_shirtseccolor,national_shirtpattern, nationalU21_team_id,nationalU21_shirt_no,nationalU21_shirtpricolor,nationalU21_shirtseccolor,nationalU21_shirtpattern,team_name,player_name,t_salary,retiring,retiring_date,tc_cur_season,status,origin_country_id,origin_country_shortname,origin_country_name,origin_country_translated_name,skills,flags,tr_bids,attributes,stats,shirtnr,show_on_tactics,value,salary,age,ontransfer,sell,transferarray,shirtpricolor,shirtseccolor,shirtpattern,junior, shortlist,currency_suffix,currency_id,mz_currency_id,injury,training_camp,warning)
{

  Player.call(this,player_id,team_id,national_team_id,national_shirt_no,national_shirtpricolor,national_shirtseccolor,national_shirtpattern, nationalU21_team_id,nationalU21_shirt_no,nationalU21_shirtpricolor,nationalU21_shirtseccolor,nationalU21_shirtpattern,team_name,player_name,t_salary,retiring,retiring_date,tc_cur_season,status,origin_country_id,origin_country_shortname,origin_country_name,origin_country_translated_name,skills,flags,tr_bids,attributes,stats,shirtnr,show_on_tactics,value,salary,age,ontransfer,sell,transferarray,shirtpricolor,shirtseccolor,shirtpattern,junior, shortlist,currency_suffix,currency_id,mz_currency_id,injury,training_camp,warning);
	
  if (this.status == 'employed') {
    if (this.tc_cur_season == 'y') {
      this.tcThisSeasonInfo = '<td align=right></td>';
      this.tcThisSeasonInfo = doTheReplace(this.tcThisSeasonInfo, this );
      this.tcThisIcon = '<img src="img/icon_trainingcamp_dg.gif" >';
    }
    else {
      this.tcThisSeasonInfo = '';
      this.tcThisIcon = '<img src="img/icon_trainingcamp_bw.gif" style="-moz-opacity:0.40; filter:alpha(opacity=40); opacity:0.4;">';
    }
  }
  else
    this.tcThisIcon = '';
	var date = new Date();

	if ( this.junior ) {
		this.strsalary = p_strjunior;
		this.salary_display = "";
		this.exchanged_salary_display = "";
	}
	else{
		this.strsalary += ":";
		this.exchanged_salary_display += " " + user_currency_suffix;
	}
	
	var flashSport = 'football';
	if (sport == 'hockey') {
    flashSport = 'hockey';
	}

	this.shirtflash = getFlashString('jersey_viewer', 'swf/' + p_garmentFlashFilename, p_garmentFlashSizeWidth, p_garmentFlashSizeHeight, 'transparent', 
                                   'pos_x=-8&pos_y=-5&browser=other&game_mode='+flashSport+'&shirt_only=' + p_garmentFlashShirtOnly + '&uqid='+uqid+'&identifier=' + date.getTime() + '_' + this.player_id + '&scale=0.5&shirt_number=' + this.shirtnr + '&' +
                                   'shirt1=' + this.shirtpricolor + '&shirt2=' + this.shirtseccolor + '&pattern_type=' + (this.shirtpattern - 1) + '&pants1=' + p_shorts1 + '&pants2=' + p_shorts2 + '&socks1=' + p_socks1 + '&socks2=' + p_socks2,
                                   'd27cdb6e-ae6d-11cf-96b8-444553540000',	
                                   'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0', "swf/", '#F2F1EC');
}

soccerPlayer.prototype = new Player();
soccerPlayer.prototype.toString = function() { return "soccerPlayer"; };
soccerPlayer.prototype.setDGColors = function(skin, hair, facial)
{
	this.skin_color = skin;
	this.hair_color = hair;
	this.hair_facial_color = hair;	//same color for hair and facial right now.
}
soccerPlayer.prototype.addAppearanceButton = function()
{
	if ( p_steamid == this.team_id )
		return doTheReplace( this.appearanceButton, this );
	else 
		return "";
}
soccerPlayer.prototype.setDigitalGoods = function(items)
{
  this.digitalGoodsArray = items;	//save if we want to use somewhere else.
  var goodsFlashVars = "&";
  for ( var i = 0; i < items.length; ++i ) {
    //items[i][0] = slotname
    //items[i][1] = swf path
    goodsFlashVars += items[i][0] + "=" + (( !items[i][1] ) ? "-1" : items[i][1]) + "&";
  }
  goodsFlashVars += "skin_color=" + this.skin_color + "&hair_color=" + this.hair_color + "&hair_facial_color=" + this.hair_facial_color;
  var date = new Date();
  this.shirtflash = getFlashString('jersey_viewer', 'swf/' + p_garmentFlashFilename, p_garmentFlashSizeWidth, p_garmentFlashSizeHeight, 'transparent', 
                                   'pos_x=-8&pos_y=-5&browser=other&game_mode=soccer&shirt_only=' + p_garmentFlashShirtOnly + '&uqid='+uqid+'&identifier=' + date.getTime() + '_' + this.player_id + '&scale=0.5&shirt_number=' + this.shirtnr + '&' +
                                   'shirt1=' + this.shirtpricolor + '&shirt2=' + this.shirtseccolor + '&pattern_type=' + (this.shirtpattern - 1) + '&pants1=' + p_shorts1 + '&pants2=' + p_shorts2 + '&socks1=' + p_socks1 + '&socks2=' + p_socks2 + goodsFlashVars,
                                   'd27cdb6e-ae6d-11cf-96b8-444553540000',	
                                   'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0', "swf/", '#F2F1EC');
}
soccerPlayer.prototype.fetchNationalLinks = function(national)
{
  var p = this;
  var plink = new Array();
  if (national == 1) { //Remove senior player
    plink[plink.length] = '<br><a target=_parent href="?p=national_teams_admin&sub=players&pid=' + p.player_id + '&extra=remove">'+strRemove+'</a>';
  }
  else if (national == 2 && p.status != "retired") {//Adding Senior player
    plink[plink.length] = '<br><a target=_parent href="?p=national_teams_admin&sub=players&pid=' + p.player_id + '&extra=add">'+strAdd+'</a>';
  }
  else if (national == 3 && p.status != "retired") { //Remove U-21 player or copy it to senior team
    plink[plink.length] = '<br><a target=_parent href="?p=national_teams_admin&type=u21&sub=players&pid=' + p.player_id + '&extra=remove">'+strRemoveU21+'</a>';
    plink[plink.length] = '<a target=_parent href="?p=national_teams_admin&sub=players&pid=' + p.player_id + '&extra=add">'+strToSeniors+'</a>';
  }
  else if (national == 4 && p.status != "retired") {//Adding U-21 player
    plink[plink.length] = '<br><a target=_parent href="?p=national_teams_admin&type=u21&sub=players&pid=' + p.player_id + '&extra=add">'+strAddU21+'</a>';
  }
  else if (national == 5 && p.status != "retired") { //Adding both senior and U-21 player
    plink[plink.length] = '<br><a target=_parent href="?p=national_teams_admin&sub=players&pid=' + p.player_id + '&extra=add">'+strAdd+'</a>';
    plink[plink.length] = '<a target=_parent href="?p=national_teams_admin&type=u21&sub=players&pid=' + p.player_id + '&extra=add">'+strAddU21+'</a>';
  }
  else if (p.team_id == p_steamid && p.status == "employed") {
    if (p.junior == 0) {
      if (p.ontransfer) {
        plink[plink.length] = '';
        plink[plink.length] = '';
      }
      else {
        plink[plink.length] = '<a target=_parent href="?p=players&pid=' + p.player_id + '&extra=sell"><img src=\"img/sell.gif\" border=\"0\" ' +getAltTitle(p_strsell)+ ' ></a>';

        if (clubMember)
          plink[plink.length] = '<a target=_parent href="?p=players&sub=notes&pid=' + p.player_id + '"><img src=\"img/notes.gif\" border=\"0\" ' +getAltTitle(p_strnotes)+ ' ></a>';

        plink[plink.length] = '<a target=_parent href="?p=players&pid=' + p.player_id + '&extra=fire"><img src=\"img/discard.gif\" border=\"0\" ' +getAltTitle(p_strfire)+ ' ></a>';

      }
    }
    else {
      //plink[plink.length] = p_strjunior;
      plink[plink.length] = "";
      if (clubMember) {
        plink[plink.length] = '<a target=_parent href="?p=players&sub=notes&pid=' + p.player_id + '"><img src=\"img/notes.gif\" border=\"0\" ' +getAltTitle(p_strnotes)+ ' ></a>';
      }

    }
  }
  return plink;
}
soccerPlayer.prototype.toHTML = function(national)
{
  plinks = "";
  tr_links = "";
  tr_bids_str = "";
  var p = this;

  var tr_link = new Array();
  var plink = new Array();

  var nationalLinks = this.fetchNationalLinks(national);
  for ( var i = 0; i < nationalLinks.length; ++i ) {
    plink.push( nationalLinks[i] );
  }

  if (p.ontransfer && p.team_id != p_steamid) {
    var amount = 0;
    if (p.tr_lastbid == 0) {
      amount = p.tr_startbid;
      amount = exchange(p.mz_currency_id,
                        p.currency_id,
                        amount,
                        exchange_rates);
    }
    else {
      amount = Math.ceil(p.tr_lastbid*1.05);
      amount = exchangeCeil(p.mz_currency_id,
                            p.currency_id,
                            amount,
                            exchange_rates);
    }
    /* We have to replace quotes within the name to avoid Javascript crashes */
    var temp_str = p.player_name;
    var temp_str2 = temp_str.replace(/\'/g,'`'); //Replacing ' with ^ /g means global => change all instances of single quote
    var JSplayer_name = temp_str2;
    var buylink = "<a target=_parent title=\""+p_strbuyplayer+"\" href=\"javascript: parent.buy(" + amount + ", " + p.value + ", " + p.player_id + ", " + p.currency_id  + ", " + user_currency_id  +  " , '" + p.currency_suffix + "', '" + user_currency_suffix  + "', " + p.value + ", " + p.salary + ", " + p.t_salary + ", " + p.warning + ", '" + JSplayer_name + "');\"><img src=\"img/sell.gif\" border=\"0\">";

    plink[plink.length] = buylink + "</a>";
    tr_link[tr_link.length] = buylink +"</a>";
  }
  
  if (p.ontransfer) {
    if (p.tr_bids.length != 0) {
      for (var i=0; i<p.tr_bids.length; i++) {
        bid_value = exchange(p.mz_currency_id,
                             p.tr_bids[i][3],
                             p.tr_bids[i][0],
                             exchange_rates);

        exch_bid_value = exchange(p.mz_currency_id,
                                  user_currency_id,
                                  p.tr_bids[i][0],
                                  exchange_rates);


        tr_bids_str += "<span title=\""+ presentMoney(bid_value) +" "+ p.tr_bids[i][4] +"\">"+presentMoney(exch_bid_value)+" "+ user_currency_suffix +"</span>, "+p.tr_bids[i][2]+"<br>";

      }
    }
    else
      tr_bids_str = "No bids yet";
  }


  if (p.status != "retired") {
    if (p.shortlist == 1) {
      plink[plink.length] = '<a target=_parent href="?p=shortlist&sub=remove&pid=' + p.player_id + '"><img src=\"img/monitor_remove.gif\" border=\"0\"  ' +getAltTitle(p_strremove)+ ' ></a>';
    }
    else {
      /* switch place at discard icon and monitor icon */
      var index = plink.length-1;
      var discard = plink[index];

      if (p.status == "employed") {
        if ( index == -1 )
          plink[0] = '<a target=_parent href="?p=players&sub=monitor&pid=' + p.player_id + '&extra=transfer"><img src=\"img/monitor.gif\" border=\"0\"  ' +getAltTitle(p_strmonitor)+ ' /></a>';
        else
          plink[index] = '<a target=_parent href="?p=players&sub=monitor&pid=' + p.player_id + '&extra=transfer"><img src=\"img/monitor.gif\" border=\"0\"  ' +getAltTitle(p_strmonitor)+ ' /></a>';
      }

      if ( typeof(discard) != "undefined" && discard && index != -1 ) {
        plink[index+1] = discard;
      }
    }
    tr_link[tr_link.length] = '<a target=_parent title=\"' +getAltTitle(p_strmonitor)+ '\" href="?p=players&sub=monitor&pid=' + p.player_id + '"><img src=\"img/monitor.gif\" border=\"0\" ' +getAltTitle(p_strmonitor)+ '></a>';
  }
  else if (!p_isRetiredPlayersPage) {
    plink[plink.length] = '<a target=_parent href="?p=shortlist&sub=remove&pid=' + p.player_id + '"><img src=\"img/monitor_remove.gif\" border=\"0\"  ' +getAltTitle(p_strremove)+ ' ></a>';  
    //We don't want to display the icon to remove a player if we are on Retired Player Page, but we want to if we are at Monitoring Page
  }
  // Shirts. If nationalteampage and player is nationalTeamPlayer the shirt 
  // should be cached on national_team_id. Otherwise on team_id.
  if ((p_isNationalCoachPage || p_isNationalTeamPage) && 
      p.national_team_id != 0) {
    p.shirtcache_team_id = p.national_team_id;
  }
  else {
    p.shirtcache_team_id = p.team_id;
  }

  if (p.junior == 2) { //trainer
    plink = new Array();
    if (p.team_id == p_steamid)
      plink[plink.length] = '<a target=_parent href="?p=trainers&cid=' + p.player_id + '&extra=fire"><img src=\"img/discard.gif\" border=\"0\" ' +getAltTitle(p_strfire)+ ' ></a>';
  }
  
  if (p_isNationalCoach && !p_isNationalCoachPage && 
      p.origin_country_id == p_user_origin_country_id && p.status == "employed") {
    plink[plink.length] = '<a target=_parent href="?p=national_teams_admin&sub=search&pid=' + p.player_id + '">'+strView+'</a>';
  }

  var template = p_playerTemplate;


  if (isSpecialPlayer(p.team_id)) {
    plink = new Array();
  }

  for (var i=0; i<plink.length; i++) {
    plinks += p_breaksBeforeLinks + plink[i];
  }

  for (var i=0; i<tr_link.length; i++) {
    tr_links += "&nbsp;" + tr_link[i];
  }

  return doTheReplace(template, this );
}
//soccerPlayer.prototype.generateRetireInfo = function()
//{

  // if ( this.retiring) {
//     if (this.ontransfer)
    //   return '<div class="retireinfo">'+str_retiring +' ('+ this.retiring_date + ')</div>';
     /*else
       return "<tr><td colspan=3 width=1 height=6 valign=top><img src=\"img/trans.gif\" width=1 height=6 border=0></td></tr>" +
	 "<tr><td width=1 colspan=5 valign=top class=" + p_windowbg + "><img src=\"img/trans.gif\" width=1 height=1 border=0></td></tr>" +
	 "<tr><td colspan=3 width=1 height=6 valign=top><img src=\"img/trans.gif\" width=1 height=6 border=0></td></tr>" +
	 "<tr><td style=\"background-color: red; height: 25px; color: #FFFFFF;\" align=center>&nbsp;<strong>" + str_retiring +" ("+ this.retiring_date + ")</strong></td></tr>";*/
   //}
//  return "";
//}

function hockeyPlayer(player_id,team_id,national_team_id,national_shirt_no,national_shirtpricolor,national_shirtseccolor,national_shirtpattern, nationalU21_team_id,nationalU21_shirt_no,nationalU21_shirtpricolor,nationalU21_shirtseccolor,nationalU21_shirtpattern,team_name,player_name,t_salary,retiring,retiring_date,tc_cur_season,status,origin_country_id,origin_country_shortname,origin_country_name,origin_country_translated_name,skills,flags,tr_bids,attributes,stats,shirtnr,show_on_tactics,value,salary,age,ontransfer,sell,transferarray,shirtpricolor,shirtseccolor,shirtpattern,junior, shortlist,currency_suffix,currency_id,mz_currency_id,injury,training_camp,warning)
{
  Player.call(this,player_id,team_id,national_team_id,national_shirt_no,national_shirtpricolor,national_shirtseccolor,national_shirtpattern, nationalU21_team_id,nationalU21_shirt_no,nationalU21_shirtpricolor,nationalU21_shirtseccolor,nationalU21_shirtpattern,team_name,player_name,t_salary,retiring,retiring_date,tc_cur_season,status,origin_country_id,origin_country_shortname,origin_country_name,origin_country_translated_name,skills,flags,tr_bids,attributes,stats,shirtnr,show_on_tactics,value,salary,age,ontransfer,sell,transferarray,shirtpricolor,shirtseccolor,shirtpattern,junior, shortlist,currency_suffix,currency_id,mz_currency_id,injury,training_camp,warning);
}
hockeyPlayer.prototype.toString = function() { return "hockeyPlayer"; };
hockeyPlayer.prototype = new Player();



function MonitorPlayer(team_id,
                       player_id,
                       player_name,
                       value,
                       salary,
                       t_salary,
                       origin_country_shortname,
                       currency_id,
                       currency_suffix,
                       last_bid,
                       bidder_team_id,
                       deadline,
                       my_team_id,
                       buy_icon)
{
  this.team_id = team_id;
  this.player_id = player_id;
  this.player_name = player_name;
  
  var spaceIndex = this.player_name.indexOf(" ");
  if (spaceIndex >= 0  )
    this.monitor_name = this.player_name.substr(0,1) +"."+ this.player_name.substr( spaceIndex );
  else
    this.monitor_name = this.player_name;

  this.value = value;
  this.salary = salary;
  this.t_salary = t_salary;
  this.value_display = presentMoney(value);
  this.salary_display = presentMoney(salary);

  this.origin_country_shortname = origin_country_shortname.toLowerCase();
  this.currency_id = currency_id;
  this.currency_suffix = currency_suffix;

  this.tr_lastbid = last_bid;
  this.tr_lastbid_display = presentMoney(this.tr_lastbid);
  this.tr_exchanged_lastbid_display = presentMoney(exchange(50,
                                                            user_currency_id,
                                                            this.tr_lastbid,
                                                            exchange_rates));

  this.bidder_team_id = bidder_team_id;

  this.tr_enddate = deadline;
  this.my_team_id = my_team_id;

  this.type = "Buy";
  if (this.my_team_id == this.team_id)
    this.type = "Sell";

  this.is_leader = false;
  if (this.my_team_id == this.bidder_team_id)
    this.is_leader = true;

  this.buy_icon = buy_icon;
  this.buy_link = "";
  if (this.type == "Buy")
    if (this.is_leader)
      this.buy_link = "<a href='#'>"+ this.buy_icon +"</a>";
    else if (!this.is_leader)
      this.buy_link = "<a href='`?p=transfer&sub=bid&pid="+ this.player_id +"'>"+ this.buy_icon +"</a>";

  this.title = this.type +": "+ this.player_name +" ("+ this.player_id +") "+ this.tr_exchanged_lastbid_display +" "+ this.currency_suffix +" @ "+ this.tr_enddate;

}

function presentMoney(_money)
{
  if ( typeof(_money) == "undefined" )
    return "";
  var money = _money.toString();
  var result = "";
  var index = 0;
  while (money.length > 0) {
    index = money.length - 3;
    if (index < 0)
      index = 0;
    result = money.substr(index) + " " + result;
    money = money.substr(0, index);
  }
  if (result.length >= 1)
    result = result.substr(0, result.length-1);
  return result;
} 

function isSpecialPlayer(tid)
{
  var x = false;
  if (tid == 79051 || 
      tid == 79052 || 
      tid == 79053 || 
      tid == 79054 || 
      tid == 111211 || 
      tid == 131549 || 
      tid == 51 ||
      tid == 52 || 
      tid == 53)
    x = true;
  return x;
}
