var ie4=document.all;
var ns6=document.getElementById&&!document.all;

var varStartMoney = 50000000;
var arrPlayers = new Array();
var arrTransferPlayers = new Array();
var TeamId = 0;

var intTransferMidfielderCount = 0;
var intTransferStrikerCount = 0;
var intTransferDefenderCount = 0;
var intTransferGoalkeeperCount = 0;

var TransferCount = 0;

/*if (top.location.href!=self.location.href)
  top.location.href=self.location.href;*/

var xmlhttp;

function xmlInit()
{
  xmlhttp=false;
  
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
  xmlhttp = new XMLHttpRequest();
}


function fnGetServerVars(strPage, fnCallback)
{
  xmlInit();
  xmlhttp.open("GET", strPage, true);
  xmlhttp.onreadystatechange=fnCallback;
  xmlhttp.send(null)
}


function fnDisplayPlayerDetails(strId)
{
  fnGetServerVars("ajax.php?mode=player&id="+strId, fnPlayerDetails_callback);
}


function fnPlayerDetails_callback()
{
  if (xmlhttp.readyState==4)
    fnFindBrowserObject("playerDetails").innerHTML = xmlhttp.responseText;
}


function fnAddPlayer(intID, strName, lngValue, strPosition, blnCreatingNew)
{
  idFound = -1;
  
  for (intIndex = 0 ; intIndex < arrPlayers.length ; intIndex++)
    if (arrPlayers[intIndex][0]==intID)
      idFound = intIndex;
      
  
  if (idFound!=-1)
  {
    fnDel(idFound, blnCreatingNew)
  }
  else
  {
    if (!blnCreatingNew)
    {
      TransferCount++;
      fnFindBrowserObject('transferCount').innerHTML = TransferCount;
      
      switch(strPosition.toLowerCase())
      {
        case "striker" : intTransferStrikerCount--; break;
        case "midfielder" : intTransferMidfielderCount--; break;
        case "defender" : intTransferDefenderCount--; break;
        case "goalkeeper" : intTransferGoalkeeperCount--; break;
      }
    }
    
    arrPlayers.push(new Array(intID, strName, lngValue, strPosition));
    
    varStartMoney -= lngValue;
    
    fnFindBrowserStyle(fnFindBrowserObject("tdPlName"+intID)).backgroundColor = "#FFF1BD";
    fnFindBrowserStyle(fnFindBrowserObject("tdPlClubName"+intID)).backgroundColor = "#FFF1BD";
    fnFindBrowserStyle(fnFindBrowserObject("tdPlHistPoints"+intID)).backgroundColor = "#FFF1BD";
	fnFindBrowserStyle(fnFindBrowserObject("tdPlPoints"+intID)).backgroundColor = "#FFF1BD";
    fnFindBrowserStyle(fnFindBrowserObject("tdPlValue"+intID)).backgroundColor = "#FFF1BD";
    fnFindBrowserStyle(fnFindBrowserObject("tdPlAdd"+intID)).backgroundColor = "#FFF1BD";
    fnFindBrowserObject("tdPlButAdd"+intID).value="rem";

    fnWritePlayerList(blnCreatingNew);
  }
}


function fnDel(intIdent, blnCreatingNew)
{
  varStartMoney += arrPlayers[intIdent][2];
  
  if (!(blnCreatingNew))
  {
    TransferCount--;
    fnFindBrowserObject('transferCount').innerHTML = TransferCount;
      
    switch(arrPlayers[intIdent][3].toLowerCase())
    {
      case "striker" : intTransferStrikerCount++; break;
      case "midfielder" : intTransferMidfielderCount++; break;
      case "defender" : intTransferDefenderCount++; break;
      case "goalkeeper" : intTransferGoalkeeperCount++; break;
    }        
  }
  
  fnFindBrowserStyle(fnFindBrowserObject("tdPlName"+arrPlayers[intIdent][0])).backgroundColor = "#ffffff";
  fnFindBrowserStyle(fnFindBrowserObject("tdPlClubName"+arrPlayers[intIdent][0])).backgroundColor = "#ffffff";
  fnFindBrowserStyle(fnFindBrowserObject("tdPlHistPoints"+arrPlayers[intIdent][0])).backgroundColor = "#ffffff";
  fnFindBrowserStyle(fnFindBrowserObject("tdPlPoints"+arrPlayers[intIdent][0])).backgroundColor = "#ffffff";
  fnFindBrowserStyle(fnFindBrowserObject("tdPlValue"+arrPlayers[intIdent][0])).backgroundColor = "#ffffff";
  fnFindBrowserStyle(fnFindBrowserObject("tdPlAdd"+arrPlayers[intIdent][0])).backgroundColor = "#ffffff";
  fnFindBrowserObject("tdPlButAdd"+arrPlayers[intIdent][0]).value="add";
  
  arrFirst = new Array();
  if (intIdent > 0) arrFirst = arrPlayers.slice(0,intIdent);
  arrSecond = arrPlayers.slice(intIdent+1);
  
  arrPlayers = arrFirst.concat(arrSecond);
  fnWritePlayerList(blnCreatingNew);
}


function parseCurrency(fltValue, strDefault, intPrecision, blnAllowblank)
{
  if (fltValue=='')
    return (blnAllowblank) ? '' : strDefault;
  else
  {
    fltValue = fltValue.replace(/ /g, "");      
    fltValue = fltValue.replace(/,/g, "");        
    fltValue = parseFloat(fltValue);
    
    if (isNaN(fltValue)) 
      return(strDefault);
    else  
      return (fltValue.toFixed(intPrecision));
  }
}


function fnCheckTransferCount()
{
  strAddError = "";
  
  if (TransferCount>12) strAddError += "<li>You have reached the maximum transfer limit of twelve transfers</li>";
  
  if (intTransferStrikerCount!=0) strAddError += (intTransferStrikerCount>0) ? "<li>There are not enough strikers in your new team</li>" : "<li>There are too many strikers in your new team</li>";
  if (intTransferMidfielderCount!=0) strAddError += (intTransferMidfielderCount>0) ? "<li>There are not enough midfielders in your new team</li>" : "<li>There are too many midfielders in your new team</li>";
  if (intTransferGoalkeeperCount!=0) strAddError += (intTransferGoalkeeperCount>0) ? "<li>You new team does not have a goalkeeper!</li>" : "<li>You have selected too many goalkeepers for your new team</li>";
  if (intTransferDefenderCount!=0) strAddError += (intTransferDefenderCount>0) ? "<li>There are not enough defenders in your new team</li>" : "<li>There are too many defenders in your new team</li>";
  
  return (strAddError);
}


function fnWritePlayerList(blnCreatingNew)
{
  strPlayerTable="";
  strForm="";
  strError = "";
  Transfer_out="";
  
  if (varStartMoney<0)
  {
    if (blnCreatingNew)
      strError="<li>This team exceeds your maximum spend amount. You cannot create this team</li>";
    else
      strError="<li>These players exceed your remaining funds, you cannot make these transfers</li>";
    
    fnFindBrowserStyle(fnFindBrowserObject('divMoneyRemaining')).color = "#bb0000";
  }
  else
    fnFindBrowserStyle(fnFindBrowserObject('divMoneyRemaining')).color = "#4A5561";
  
  if (blnCreatingNew) 
    strError += fnCheckFormation();
  else
    strError += fnCheckTransferCount();
  
  if (strError!="") strPlayerTable += "<div class=\"warning\"><ul>"+strError+"</ul></div>";
    
  strPlayerTable += "<table id='teamTable' cellspacing='1'>";
  strPlayerTable += "<tr><th>&nbsp;</th><th>player name:</th><th>cost:</th><th>position</th><th>&nbsp;</th></tr>";
  
  for (intIndex = 0 ; intIndex < arrPlayers.length ; intIndex++)
  {
    strPlayerTable += "<tr><td>"+(intIndex+1)+"</td><td class=\"playerchooser\" onclick=\"fnDisplayPlayerDetails("+arrPlayers[intIndex][0]+");\"><strong>"+arrPlayers[intIndex][1]+"</strong></td><td class=\"playerchooser\" onclick=\"fnDisplayPlayerDetails("+arrPlayers[intIndex][0]+");\">&pound;"+((arrPlayers[intIndex][2] / 1000000).toFixed(1))+"m</td><td class=\"playerchooser\" onclick=\"fnDisplayPlayerDetails("+arrPlayers[intIndex][0]+");\">"+arrPlayers[intIndex][3]+"</td><td><input type=\"button\" value=\"remove\" class=\"but_remove\" onclick=\"fnDel("+intIndex+", "+blnCreatingNew+");\" /></td></tr>";
    strForm += arrPlayers[intIndex][0]+",";
  }
  
  if (blnCreatingNew)
  {
    strPlayerTable += "</table><form method=\"post\" action=\"?mode=submitteam\" name=\"fmrSubmit\"><input type=\"hidden\" name=\"team\" value=\""+strForm+"\" /><input type=\"hidden\" name=\"startmoney\" value=\""+varStartMoney+"\" /><input type=\"hidden\" name=\"formation\" value=\""+fnFindBrowserObject('dropFormation').selectedIndex+"\" />";
    
    if ((strError!="")||(arrPlayers.length!=11)) //
      strPlayerTable += "<input type=\"submit\" id=\"butTeamSubmit\" value=\"create team\" disabled=\"true\" /></form>";
    else
      strPlayerTable += "<input type=\"submit\" id=\"butTeamSubmit\" value=\"create team\" /></form>";
  }
  else
  {
    for (intIndex = 0 ; intIndex < arrTransferPlayers.length ; intIndex++)
      Transfer_out += arrTransferPlayers[intIndex]+",";
    
    strPlayerTable += "</table><form method=\"post\" action=\"?mode=maketransfer\" name=\"fmrSubmit\"><input type=\"hidden\" name=\"transfer_in\" value=\""+strForm+"\" /><input type=\"hidden\" name=\"transfer_out\" value=\""+Transfer_out+"\" /><input type=\"hidden\" name=\"moneyremaining\" value=\""+varStartMoney+"\" /><input type=\"hidden\" name=\"transfercount\" value=\""+TransferCount+"\" /><input type=\"hidden\" name=\"team_id\" value=\""+TeamId+"\" /><input type=\"hidden\" name=\"formation\" value=\""+fnFindBrowserObject('dropFormation').selectedIndex+"\" />";
    
    if (strError!="")//
      strPlayerTable += "<input type=\"submit\" id=\"butTeamSubmit\" value=\"make transfers\" disabled=\"true\" /></form>";
    else
      strPlayerTable += "<input type=\"submit\" id=\"butTeamSubmit\" value=\"make transfers\" /></form>";
  }
    
  fnFindBrowserObject('divTeam').innerHTML = strPlayerTable;
  fnFindBrowserObject('divMoneyRemaining').innerHTML = "&pound;"+(varStartMoney / 1000000).toFixed(1)+" million";
}


function fnInitFormation(blnCreatingNew)
{  
   switch (fnFindBrowserObject('dropFormation').selectedIndex)
   {
	 case 0 : // 4-4-2
	   if (blnCreatingNew)
	   {
		   fnFindBrowserStyle(fnFindBrowserObject('formation5')).top="16px";
		   fnFindBrowserStyle(fnFindBrowserObject('formation6')).top="68px"; 
		   fnFindBrowserStyle(fnFindBrowserObject('formation7')).top="120px"; 
		   fnFindBrowserStyle(fnFindBrowserObject('formation8')).top="172px";
	  
		   fnFindBrowserStyle(fnFindBrowserObject('formation9')).top="52px"; 
		   fnFindBrowserStyle(fnFindBrowserObject('formation10')).top="132px"; 
		   
		   fnFindBrowserStyle(fnFindBrowserObject('formation5')).marginLeft="4px";  
		   fnFindBrowserStyle(fnFindBrowserObject('formation8')).marginLeft="4px";
	   }
	   else
	   {
		   intTransferStrikerCount--;
		   intTransferMidfielderCount++;		   
	   }
	  break;
	 default : //4-3-3
 	   if (blnCreatingNew)
	   {		   
		   fnFindBrowserStyle(fnFindBrowserObject('formation5')).top="42px";
		   fnFindBrowserStyle(fnFindBrowserObject('formation6')).top="92px"; 
		   fnFindBrowserStyle(fnFindBrowserObject('formation7')).top="142px"; 
		   
		   fnFindBrowserStyle(fnFindBrowserObject('formation8')).top="22px";
		   fnFindBrowserStyle(fnFindBrowserObject('formation9')).top="92px"; 
		   fnFindBrowserStyle(fnFindBrowserObject('formation10')).top="162px";
		   
		   fnFindBrowserStyle(fnFindBrowserObject('formation5')).marginLeft="14px"; 
		   fnFindBrowserStyle(fnFindBrowserObject('formation8')).marginLeft="110px";
	   }
	   else
	   {
		   intTransferStrikerCount++;
		   intTransferMidfielderCount--;		   
	   }
	 break; 
   }
}


function fnCheckFormation()
{
  var strAddError = "";
  var intGoalKeeperCount = 0;
  var intDefenderCount = 0;
  var intMidfieldCount = 0;
  var intStrikerCount = 0;
  var blnGoalWarn = false;
  var blnDefenderWarn = false;
  var blnMidefielderWarn = false;
  var blnStrikerWarn = false;
  
  for (intIndex = 0 ; intIndex <= 10 ; intIndex++)
    fnFindBrowserObject('formation'+intIndex).innerHTML = "";
  
  for (intIndex = 0 ; intIndex < arrPlayers.length ; intIndex++)
  {
    switch(arrPlayers[intIndex][3])
    {
      case "Goalkeeper" : 
        intGoalKeeperCount++;
        if ((intGoalKeeperCount>1)&&(!(blnGoalWarn)))
        {
          blnGoalWarn = true;
          strAddError+="<li>You have selected too many goalkeepers</li>";
        }
        else
        {
          if (!blnGoalWarn)
            fnFindBrowserObject('formation0').innerHTML = arrPlayers[intIndex][1];
        }
        break;
      case "Defender" : 
        intDefenderCount++;
        if ((intDefenderCount>4)&&(!(blnDefenderWarn)))
        {
          blnDefenderWarn = true;
          strAddError+="<li>You have selected too many defenders</li>";
        }
        else
        {
          if (!blnDefenderWarn)
            fnFindBrowserObject('formation'+intDefenderCount).innerHTML = arrPlayers[intIndex][1];
        }
        break;
      case "Midfielder" :
        intMidfieldCount++
        if ((((fnFindBrowserObject('dropFormation').selectedIndex==0)&&(intMidfieldCount>4))||((fnFindBrowserObject('dropFormation').selectedIndex==1)&&(intMidfieldCount>3)))&&(!(blnMidefielderWarn)))
        {
          blnMidefielderWarn = true;
          strAddError+="<li>You have selected too many midfielders for your chosen formation</li>";
        }
        else
        {
          if (!blnMidefielderWarn)
            fnFindBrowserObject('formation'+(intMidfieldCount+4)).innerHTML = arrPlayers[intIndex][1];
        }
        break;
      case "Striker" : 
        intStrikerCount++;
        if ((((fnFindBrowserObject('dropFormation').selectedIndex==0)&&(intStrikerCount>2))||((fnFindBrowserObject('dropFormation').selectedIndex==1)&&(intStrikerCount>3)))&&(!(blnStrikerWarn)))
        {
          blnStrikerWarn = true;
          strAddError+="<li>You have selected too many strikers for your chosen formation</li>";
        }
        else
        {
          if (!(blnStrikerWarn))
          {
            if (fnFindBrowserObject('dropFormation').selectedIndex==0)
              fnFindBrowserObject('formation'+(intStrikerCount+8)).innerHTML = arrPlayers[intIndex][1];
            else
              fnFindBrowserObject('formation'+(intStrikerCount+7)).innerHTML = arrPlayers[intIndex][1];
          }
        }
      break;
    }
  }
    
  return (strAddError);
}


function fnSwitchView (TabObj, TableObjToDisplay)
{
  fnResetAll();
  
  fnFindBrowserStyle(fnFindBrowserObject(TabObj)).fontWeight="bold";
  fnFindBrowserStyle(fnFindBrowserObject(TabObj)).backgroundColor="#FFEEB1";
  fnFindBrowserStyle(fnFindBrowserObject(TableObjToDisplay)).display="block";
}

function fnResetAll()
{
  fnFindBrowserStyle(fnFindBrowserObject('tabChooserGoalkeeper')).fontWeight="normal";
  fnFindBrowserStyle(fnFindBrowserObject('tabChooserDefender')).fontWeight="normal";
  fnFindBrowserStyle(fnFindBrowserObject('tabChooserMidfielder')).fontWeight="normal";
  fnFindBrowserStyle(fnFindBrowserObject('tabChooserStriker')).fontWeight="normal";
  fnFindBrowserStyle(fnFindBrowserObject('tabChooserGoalkeeper')).backgroundColor="#ffffff";
  fnFindBrowserStyle(fnFindBrowserObject('tabChooserDefender')).backgroundColor="#ffffff";
  fnFindBrowserStyle(fnFindBrowserObject('tabChooserMidfielder')).backgroundColor="#ffffff";
  fnFindBrowserStyle(fnFindBrowserObject('tabChooserStriker')).backgroundColor="#ffffff";
  
  fnFindBrowserStyle(fnFindBrowserObject('tabPlayerListGoalkeeper')).display="none";
  fnFindBrowserStyle(fnFindBrowserObject('tabPlayerListDefender')).display="none";
  fnFindBrowserStyle(fnFindBrowserObject('tabPlayerListMidfielder')).display="none";
  fnFindBrowserStyle(fnFindBrowserObject('tabPlayerListStriker')).display="none";
}


function fnTransferOut(rowId, Transferred, value, position, Id)
{
  TeamId = Id;
  
  switch(position.toLowerCase())
  {
    case "striker" : intTransferStrikerCount+= (Transferred) ? 1 : -1; break;
    case "midfielder" : intTransferMidfielderCount+= (Transferred) ? 1 : -1; break;
    case "defender" : intTransferDefenderCount+= (Transferred) ? 1 : -1; break;
    case "goalkeeper" : intTransferGoalkeeperCount+= (Transferred) ? 1 : -1; break;
  }
  
  if (Transferred)
    arrTransferPlayers.push(rowId);
  else
    deleteTransferredPlayer(rowId);
  
  fnFindBrowserStyle(fnFindBrowserObject('tabletransfername'+rowId)).textDecoration=Transferred ? "line-through" : "none";
  varStartMoney += (Transferred) ? value : -value; 

  fnWritePlayerList(0);
}


function deleteTransferredPlayer(ID)
{
  intIdent=0;
  
  while(arrTransferPlayers[intIdent]!=ID)
    intIdent++;
  
  arrFirst = new Array();
  if (intIdent > 0) arrFirst = arrTransferPlayers.slice(0,intIdent);
  arrSecond = arrTransferPlayers.slice(intIdent+1);
  
  arrTransferPlayers = arrFirst.concat(arrSecond);
}


function fnFindBrowserObject(objIdent)
{ return (ie4 ? eval(objIdent) : ns6 ? document.getElementById(objIdent) : ns4 ? document.objIdent : ""); }

function fnFindBrowserStyle(objIdent)
{ return ((ie4||ns6) ? objIdent.style : objIdent); }