// Define the lookup tables - There must be one for every language
trans = new Array();
trans["en"] = new Array();
trans["zh"] = new Array();
trans["fr"] = new Array();
trans["es"] = new Array();
trans["pt"] = new Array();
trans["it"] = new Array();
trans["de"] = new Array();

// Define languages that use NCR encoding
trans["zh"]["ncr.encoded"]=1;

// Define the translations
trans["en"]["SPECIFY_TEAM_NAME"] = "Team name must be specified";
trans["zh"]["SPECIFY_TEAM_NAME"] = "&#25152;&#26377;&#29699;&#21592;&#24517;&#39035;&#36873;&#25321;";

trans["en"]["SELECT_ALL_PLAYERS"] = "All Players must be selected";
trans["zh"]["SELECT_ALL_PLAYERS"] = "&#29699;&#38431;&#21517;&#31216;&#24517;&#39035;&#25351;&#23450;";

trans["en"]["TOO_MANY_PLAYERS_IN_SIDE"] = "Sorry, you cannot pick any more players from";
trans["zh"]["TOO_MANY_PLAYERS_IN_SIDE"] = "&#24456;&#25265;&#27465;, &#24744;&#19981;&#33021;&#20877;&#36873;&#25321;&#26356;&#22810;&#29699;&#21592;&#20110; ";

trans["en"]["You can not picker more players from {1}"] = "You cannot pick any more players from {1}";

trans["en"]["ALREADY_SELECTED"] = "Oops! You have already selected ";
trans["zh"]["ALREADY_SELECTED"] = "&#22114;! &#24744;&#24050;&#32463;&#36873;&#25321;&#20102;&#36825;&#20010; ";

trans["en"]["NOT_ENOUGH_MONEY"] = "Sorry, you do not have enough money left for ";
trans["zh"]["NOT_ENOUGH_MONEY"] = "&#24456;&#25265;&#27465;, &#24744;&#27809;&#26377;&#36275;&#22815;&#37329;&#39069;&#21097;&#19979;&#21435; ";

trans["en"]["NO_MORE_TRANSFERS"] = "Sorry, you do not have any transfers left";
trans["zh"]["NO_MORE_TRANSFERS"] = "&#24456;&#25265;&#27465;, &#24744;&#27809;&#26377;&#20313;&#19979;&#30340;&#36716;&#20250;&#21517;&#39069;";

trans["en"]["UNLIMITED"] = "unlimited";
trans["zh"]["UNLIMITED"] = "&#26080;&#38480;";

trans["en"]["UNLIMITED TRANSFERS"] = "Unlimited";
trans["en"]["Unlimited Transfers"] = "Unlimited";
trans["en"]["Unlimited transfers"] = "Unlimited";
trans["en"]["unlimited transfers"] = "Unlimited";

trans["en"]["CHANGE FORMATION"] = "Choose Formation";
trans["en"]["Change Formation"] = "Choose Formation";
trans["en"]["Change formation"] = "Choose Formation";
trans["en"]["change formation"] = "Choose Formation";

trans["en"]["Choose your team"] = " ";
trans["en"]["choose your team"] = " ";
trans["en"]["CHOOSE YOUR TEAM"] = " ";

trans["en"]["TOO_MANY_TEAMS"] = "Sorry, you now have the maximum number of teams.";
trans["zh"]["TOO_MANY_TEAMS"] = "&#24456;&#25265;&#27465;, &#24744;&#29616;&#22312;&#24050;&#26377;&#21040;&#36798;&#19978;&#38480;&#30340;&#29699;&#38431;";

trans["en"]["CONFIRM_TRANSFERS"] = "Continue with {1} transfers? Active on {2}";
trans["zh"]["CONFIRM_TRANSFERS"] = "&#32487;&#32493;&#36827;&#34892; {1} &#36716;&#20250;?";

trans["en"]["NO_TEAMS"] = "You must first add some teams!";
trans["zh"]["NO_TEAMS"] = "&#24744;&#24517;&#39035;&#20808;&#22686;&#28155;&#29699;&#38431;";

trans["en"]["CONFIRM_TEAM"] = "Are you sure that you would like to continue with {1} team?";
trans["zh"]["CONFIRM_TEAM"] = "&#24744;&#26159;&#21542;&#30830;&#23450;&#36873;&#25321;{1}&#29699;&#38431;";

trans["en"]["CONFIRM_TEAMS"] = "Are you sure that you would like to continue with {1} teams?";
trans["zh"]["CONFIRM_TEAMS"] = "&#24744;&#26159;&#21542;&#30830;&#23450;&#36873;&#25321;{1}&#29699;&#38431;";
            
// Flash Game Translations                  
trans["en"]["MANAGERS NOTEBOOK"] = "MANAGER\'S NOTEBOOK";
trans["en"]["Instructions register team."] = "You must select a save option below to confirm your team changes.";
trans["en"]["Instructions seved teams."] = "You can view and edit teams saved for future game weeks here. The dates in the drop down menu tell you when a game week starts.";
trans["en"]["instructions seved teams."] = "You can view and edit teams saved for future game weeks here. The dates in the drop down menu tell you when a game week starts.";
trans["en"]["Instructions saved teams."] = "Choose which of your line-ups to view.";
trans["en"]["Instructions save team."] = "Please choose your team details";
trans["en"]["first_half_appearances"] = "1st Half Appearances";
trans["en"]["second_half_appearances"] = "2nd Half Appearances"; 
trans["en"]["goals"] = "Goals";
trans["en"]["goal_assists"] = "Assists";
trans["en"]["shots_on_target"] = "Shots on Target";
trans["en"]["accurate_cross"] = "Accurate Crosses"; 
trans["en"]["won_tackles"] = "Tackles Won";
trans["en"]["last_man_tackle"] = "Last Man Tackles";
trans["en"]["blocked_shots"] = "Blocked Shots";
trans["en"]["interceptions"] = "Interceptions";
trans["en"]["clearances"] = "Clearances"; 
trans["en"]["clean_sheet"] = "Clean Sheets";
trans["en"]["first_half_clean_sheet"] = "1st Half Clean Sheets";
trans["en"]["second_half_clean_sheet"] = "2nd Half Clean Sheets";
trans["en"]["goals_conceded"] = "Goals Conceded";
trans["en"]["pens_missed"] = "Missed Penalties";
trans["en"]["saves"] = "Saves";
trans["en"]["penalty_save"] = "Penalty Saves";
trans["en"]["yellow_card"] = "Yellow Cards";
trans["en"]["red_card"] = "Red Cards";
trans["en"]["CHANGE FORMATION"] = "Choose Formation";
trans["en"]["Change Formation"] = "Choose Formation";
trans["en"]["Change formation"] = "Choose Formation";
trans["en"]["change formation"] = "Choose Formation";
trans["en"]["Choose your team"] = " ";
trans["en"]["choose your team"] = " ";
trans["en"]["CHOOSE YOUR TEAM"] = " ";
trans["en"]["Editing team"] = "Editing team: Game week";
trans["en"]["Score"] = "Season";
trans["en"]["Monthly"] = "Month";
trans["en"]["Weekly"] = "Week";
trans["en"]["Option: 1) Save your team for the next deadline freeze."] = "Save this team for the next game week starting ";
trans["en"]["Option: 2) Save your team for a future team freeze. Ideal if you are going to be away from your computer for a while. Use the drop down boxes to select a date."] = "Option 2: Save this team for a future game week starting:";

// Define the abbreviations
trans["abbreviation"] = new Array();
trans["abbreviation"]["Manchester United"] = "MUN";
trans["abbreviation"]["Manchester City"] = "MAC";
trans["abbreviation"]["West Ham United"] = "WHM";
trans["abbreviation"]["Man City"] = "MAC";
trans["abbreviation"]["Man Utd"] = "MUN";
trans["abbreviation"]["West Ham"] = "WHM";
trans["abbreviation"]["West Brom"] = "WBA";
trans["abbreviation"]["West Bromwich Albion"] = "WBA";
trans["abbreviation"]["DEFENDER"] = "DF";
trans["abbreviation"]["DEFENSIVE MIDFIELDER"] = "DM";
trans["abbreviation"]["Defender"] = "DF";
trans["abbreviation"]["Defensive Midfielder"] = "DM";
trans["abbreviation"]["DEFENDERS"] = "DF";
trans["abbreviation"]["DEFENSIVE MIDFIELDERS"] = "DM";
trans["abbreviation"]["Goalkeeper"] = "GK";
trans["abbreviation"]["GOALKEEPER"] = "GK";
trans["abbreviation"]["Goalkeepers"] = "GK";
trans["abbreviation"]["GOALKEEPERS"] = "GK";
trans["abbreviation"]["Midfielder"] = "MF";
trans["abbreviation"]["Midfielders"] = "MF";
trans["abbreviation"]["MIDFIELDER"] = "MF";
trans["abbreviation"]["MIDFIELDERS"] = "MF";
trans["abbreviation"]["defensive midfielder"] = "DM";
trans["abbreviation"]["defensive midfielders"] = "DM";  
trans["abbreviation"]["midfielder"] = "MF";  
trans["abbreviation"]["midfielders"] = "MF";  
trans["abbreviation"]["goalkeeper"] = "GK";
trans["abbreviation"]["goalkeepers"] = "GK";
trans["abbreviation"]["defender"] = "DF";
trans["abbreviation"]["defenders"] = "DF";
trans["abbreviation"]["CHANGE FORMATION"] = "Choose Formation";
trans["abbreviation"]["Change Formation"] = "Choose Formation";
trans["abbreviation"]["Change formation"] = "Choose Formation";
trans["abbreviation"]["change formation"] = "Choose Formation";  
trans["abbreviation"]["Choose your team"] = " ";
trans["abbreviation"]["choose your team"] = " ";
trans["abbreviation"]["CHOOSE YOUR TEAM"] = " ";
trans["abbreviation"]["Weekly"] = "Week";
                
// This function will allow translating of a message
function translate(message) {
 
  var translated = undefined;

  // If the language isn't set then set it to english
  if (window.languageCode == undefined) {
   
    window.languageCode = "en";
  }

  var languageLookup = trans[window.languageCode];

  // If is a language lookup for this language then try to use it  
  if (languageLookup != undefined) {
    
    translated = languageLookup[message];
  }

  // If we haven't translated yet then translate into english
  if (languageCode != "en" &&
      translated == undefined) {

    languageLookup = trans["en"];
    translated = languageLookup[message];
  }

  // If we still haven't successfully translated then return the original message
  if (translated == undefined) {

    // console.log(message);
    translated = message;
  }

  // If this language is NCR encoded then decode it
  if (languageLookup["ncr.encoded"] == 1) {

    translated = decodeNCR(translated);
  }

  return translated;
}


// This function will replace {1} in the message with the specified variable
function translate1(message, variable) {
  
  var translated = translate(message);
  return translated.replace("{1}", variable);
}


// This function will replace {1} and {2} in the message with the specified variables
function translate2(message, variable1, variable2) {
  
  var translated = translate1(message, variable1);
  return translated.replace("{2}", variable2);
}

// This function will replace {1} and {2} and {3} in the message with the specified variables
function translate3(message, variable1, variable2, variable3) {
  
  var translated = translate2(message, variable1, variable2);
  return translated.replace("{3}", variable3);
}

function abbreviate(message, length) {

  var abbreviation = undefined;
  
  var languageLookup = trans["abbreviation"];
  if (languageLookup != undefined) {
    
    abbreviation = languageLookup[message];
  }

  // If we didn't find an abbreviation then use the original message
  if (abbreviation == undefined) {

    // console.log(message);
    abbreviation = message;
  }
  
  // Truncate the abbreviation
  if (abbreviation.length > length) {
    
    abbreviation = abbreviation.substring(0, length);
  }

  return abbreviation;
}

// This function will decode NCR encoded text 
function decodeNCR(encodedText) {

  return convertCP2Char(convertDecNCR2CP(encodedText));
}


// This function converts from a code point to a character
function convertCP2Char ( textString ) {

  var outputString = '';
  textString = textString.replace(/^\s+/, '');
  if (textString.length == 0) { return ""; }
    textString = textString.replace(/\s+/g, ' ');
  var listArray = textString.split(' ');
  for ( var i = 0; i < listArray.length; i++ ) {
    var n = parseInt(listArray[i], 16);
    if (n <= 0xFFFF) {
      outputString += String.fromCharCode(n);
    } else if (n <= 0x10FFFF) {
      n -= 0x10000
      outputString += String.fromCharCode(0xD800 | (n >> 10)) + String.fromCharCode(0xDC00 | (n & 0x3FF));
    } else {
      outputString += '!erreur ' + dec2hex(n) +'!';
    }
  }
  return( outputString );
}

// This function converts from a character to a code point
function convertChar2CP ( textString ) {

  var outputString = "";
  var haut = 0;
  var n = 0;
  for (var i = 0; i < textString.length; i++) {
    var b = textString.charCodeAt(i);  // alert('b:'+dec2hex(b));
    if (b < 0 || b > 0xFFFF) {
      outputString += '!erreur ' + dec2hex(b) + '!';
    }
    if (haut != 0) {
      if (0xDC00 <= b && b <= 0xDFFF) {
        outputString += dec2hex(0x10000 + ((haut - 0xD800) << 10) + (b - 0xDC00)) + ' ';
        haut = 0;
        continue;
      } else {
        outputString += '!erreur ' + dec2hex(haut) + '!';
        haut = 0;
      }
    }
    if (0xD800 <= b && b <= 0xDBFF) {
      haut = b;
    } else {
      outputString += dec2hex(b) + ' ';
    }
  }
  return( outputString.replace(/ $/, '') );
}

// This function converts from NCR to a code point
function convertDecNCR2CP ( textString ) {

  outputString = '';
  inNCR = false;
  tempString = '';
  for (var i=0; i<textString.length; i++) {   
    if (textString.charAt(i) == '&') { // &
      inNCR = true;
      if (tempString != '') { outputString += convertChar2CP( tempString )+' '; }
      tempString = ''; 
      }
    else if ( inNCR && textString.charAt(i) == ';' ) { // ;
      inNCR = false;
      outputString += dec2hex(parseInt(tempString.substring(1, tempString.length), 10))+' ';
      tempString = '';
      }
    else {
      tempString += textString.charAt(i);
      }
    }
  outputString = outputString.substring(0, outputString.length-1);
  if (tempString != '') { outputString += ' '+convertChar2CP( tempString ); }
  return( outputString );
}

// This function converts from decimal to hex
function dec2hex ( textString ) {

   return (textString+0).toString(16).toUpperCase();
}
