/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Down Home Consulting :: http://downhomeconsulting.com */

/*
league team Drop Downs v1.0.
(c) Copyright 2005 Down Home Consulting, Inc.
www.DownHomeConsulting.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, itness for a particular purpose and noninfringement. in no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.

*/

// If you have PHP you can set the post values like this

//var postteam = team;
//var postleague = '<?= $_POST["league"] ?>';
var postteam = '';
var postleague = '';

// team table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//

var team = '\
39:6145:Arsenal|\
39:6144:Aston Villa|\
39:7143:Birmingham City|\
39:6152:Blackburn Rovers|\
39:6155:Bolton Wanderers|\
39:8240:Burnley|\
39:6159:Chelsea|\
39:6149:Everton|\
39:6156:Fulham|\
39:7145:Hull City|\
39:6140:Liverpool|\
39:6158:Manchester City|\
39:6157:Manchester United|\
39:6153:Portsmouth|\
39:8246:Stoke City|\
39:7129:Sunderland|\
39:6154:Tottenham Hotspur|\
39:6143:West Ham United|\
39:6150:Wigan Athletic FC|\
39:7140:Wolverhampton|\
49:8288:1899 Hoffenheim|\
49:6126:Arminia Bielefeld|\
49:6133:Bayer Leverkusen|\
49:6123:Bayern M&#252;nchen|\
49:6122:Borussia Dortmund|\
49:6124:Eintracht Frankfurt|\
49:6128:FC Energie Cottbus|\
49:7164:FC Koln|\
49:6125:FC Schalke 04|\
49:6127:Hamburger SV|\
49:6137:Hannover 96|\
49:6138:Hertha BSC Berlin|\
49:7172:Karlsruher SC|\
49:6129:M;nchengladbach|\
49:6131:VfB Stuttgart|\
49:6134:VfL Bochum|\
49:6139:VfL Wolfsburg|\
49:6136:Werder Bremen|\
50:7130:Barnsley|\
50:8239:Blackpool F.C.|\
50:7382:Bristol City|\
50:8241:Cardiff City|\
50:6142:Charlton Athletic|\
50:7123:Coventry City|\
50:7131:Crystal Palace|\
50:7124:Derby County|\
50:8251:Doncaster Rovers|\
50:7125:Ipswich Town|\
50:6146:Middlesbrough|\
50:6151:Newcastle United|\
50:7132:Norwich City|\
50:7133:Nottingham Forest|\
50:8243:Plymouth Argyle|\
50:8244:Preston North End|\
50:7135:Queens Park Rangers|\
50:6147:Reading|\
50:6141:Sheffield United|\
50:7136:Sheffield Wednesday|\
50:7128:Southampton|\
50:8248:Swansea City|\
50:6148:Watford|\
50:7139:West Bromwich Albion|\
51:6393:AC Milan|\
51:6389:Atalanta|\
51:7147:Bologna|\
51:6404:Cagliari|\
51:6408:Catania|\
51:6398:Chievo|\
51:6390:Fiorentina|\
51:7158:Genoa|\
51:6391:Inter Milan|\
51:7149:Juventus|\
51:6392:Lazio|\
51:7150:Lecce|\
51:7151:Napoli|\
51:6403:Palermo|\
51:6395:Reggina|\
51:6396:Roma|\
51:6401:Sampdoria|\
51:6402:Siena|\
51:6399:Torino|\
51:6397:Udinese|\
54:8197:Almer;a|\
54:6161:Athletic Bilbao|\
54:6172:Atl;tico Madrid|\
54:6162:Barcelona|\
54:6165:Deportivo La Coru;a|\
54:6178:Espanyol|\
54:6174:Getafe|\
54:7185:M;laga|\
54:6176:Mallorca|\
54:7186:Numancia|\
54:6175:Osasuna|\
54:6173:Racing Santander|\
54:6168:Real Betis|\
54:6171:Real Madrid|\
54:8312:Real Sporting de Gij;n|\
54:6164:Real Zaragoza|\
54:6177:Recreativo de Huelva|\
54:6167:Sevilla|\
54:7190:Tenerife|\
54:6169:Valencia|\
54:7189:Valladolid|\
54:6170:Villarreal|\
54:8311:Xerez|\
58:6393:AC Milan|\
58:6439:AZ Alkmaar|\
58:7566:Apoel Nicosia|\
58:6145:Arsenal|\
58:6172:Atlético Madrid|\
58:6162:Barcelona|\
58:6123:Bayern Mÿnchen|\
58:6305:Besiktas|\
58:6416:Bordeaux|\
58:6448:CSKA Moscow|\
58:6159:Chelsea|\
58:10514:Debrecen|\
58:6454:Dynamo Kiev|\
58:6450:FC Porto|\
58:7428:FC Zurich|\
58:6390:Fiorentina|\
58:6391:Inter Milan|\
58:7149:Juventus|\
58:6140:Liverpool|\
58:6412:Lyon|\
58:6303:Maccabi Haifa|\
58:6157:Manchester United|\
58:6418:Marseille|\
58:6449:Olympiakos|\
58:6208:Rangers|\
58:6171:Real Madrid|\
58:7460:Rubin Kazan|\
58:6167:Sevilla|\
58:6192:Standard Liege|\
58:20019:Unirea Urziceni|\
58:6131:VfB Stuttgart|\
58:6139:VfL Wolfsburg|\
68:6413:Auxerre|\
68:6416:Bordeaux|\
68:7392:Caen|\
68:7395:Grenoble|\
68:7391:Le Havre|\
68:6426:Le Mans|\
68:6411:Lens|\
68:6415:Lille|\
68:6422:Lorient|\
68:6412:Lyon|\
68:6418:Marseille|\
68:6410:Monaco|\
68:6427:Nancy|\
68:6419:Nantes|\
68:6424:Nice|\
68:6409:Paris SG|\
68:6421:Rennes|\
68:6417:Sochaux|\
68:6423:St. Etienne|\
68:6425:Toulouse|\
68:6428:Valenciennes|\
70:6445:ADO Den Haag|\
70:6439:AZ Alkmaar|\
70:6430:Ajax|\
70:7518:De Graafschap|\
70:6442:FC Groningen|\
70:6441:FC Twente|\
70:6434:FC Utrecht|\
70:6431:Feyenoord|\
70:6446:Heracles Almelo|\
70:6429:NAC Breda|\
70:6438:NEC Nijmegen|\
70:6432:PSV Eindhoven|\
70:6440:Roda JC|\
70:6433:SC Heerenveen|\
70:6443:Sparta|\
70:6435:Vitesse|\
70:7521:Volendam|\
70:6437:Willem II|\
72:6202:Aberdeen|\
72:6207:Celtic|\
72:6199:Dundee United|\
72:6198:Falkirk|\
72:8325:Hamilton Academical|\
72:6200:Hearts|\
72:6203:Hibernian|\
72:6205:Inverness C.T.F.C|\
72:6206:Kilmarnock|\
72:6209:Motherwell|\
72:6208:Rangers|\
72:6204:St. Mirren|\
74:6194:Anderlecht|\
74:6186:Cercle Brugge|\
74:6191:Charleroi|\
74:6181:Club Brugge|\
74:6187:Excelsior Mouscron|\
74:8169:FC Dender|\
74:6180:Gent|\
74:6185:Germinal Beerschot|\
74:8168:KV Mechelen|\
74:9826:Kortrijk|\
74:6193:Lokeren|\
74:6188:Mons Bergen|\
74:6183:Racing Genk|\
74:6196:SV Roeselare|\
74:6192:Standard Liege|\
74:9827:Tubize|\
74:6197:Westerlo|\
74:6182:Zulte Waregem|\
124:7559:Al Ahly|\
124:19980:Al-Mokawloon|\
124:19981:El Gouna|\
124:19982:El Mansoura|\
124:19983:Zamalek SC|\
124:19984:El-Entag El-Harby|\
124:19985:El Geish|\
124:19986:El-Ittihad El-Iskandary|\
124:19987:El-Masry|\
124:19988:ENPPI Club|\
124:19989:Ghazl El-Mehalla|\
124:19990:Haras El Hodood|\
124:19991:Ismaily|\
124:19992:Ittihad El-Shorta|\
124:19993:Petrojet|\
124:19994:Asyut Petroleum|\
';

// league data table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var league = '\
49:Bundesliga 1|\
70:Dutch Eredivisie|\
50:English Championship|\
124:Egyptian Premier League|\
39:English Premier League|\
68:France Ligue 1|\
54:Liga Espana|\
72:Scottish Premier League|\
51:Serie A|\
58:UEFA Champions\
';


function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

// Populates the league selected with the counties from the league list
function populateleague(defaultleague) {
  if ( postleague != '' ) {
    defaultleague2 = postleague;
  }
  var leagueLineArray = league.split('|');  // Split into lines
  var selObj = document.getElementById('leagueSelect');
  selObj.options[0] = new Option('Select league','');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < leagueLineArray.length; loop++) {
    lineArray = leagueLineArray[loop].split(':');
    leagueCode  = TrimString(lineArray[0]);
    leagueName  = TrimString(lineArray[1]);
    if ( leagueCode != '' ) {
      selObj.options[loop + 1] = new Option(leagueName, leagueCode);
    }
    if ( defaultleague == leagueCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}

function populateteam(defaultTeam) {

  var selObj = document.getElementById('teamSelect');
  var foundteam = false;
  // Empty options just in case new drop down is shorter
  if ( selObj.type == 'select-one' ) {
    for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    selObj.options[0] = new Option('Select team','');
    selObj.selectedIndex = 0;
  }
  // Populate the drop down with teams from the selected league
  var teamLineArray = team.split("|");  // Split into lines
  var optionCntr = 1;
  for (var loop = 0; loop < teamLineArray.length; loop++) {
    lineArray = teamLineArray[loop].split(":");
    leagueCode  = TrimString(lineArray[0]);
    teamCode    = TrimString(lineArray[1]);
    teamName    = TrimString(lineArray[2]);
  if (document.getElementById('leagueSelect').value == leagueCode && leagueCode != '' ) {
    // If it's a input element, change it to a select
      if ( selObj.type == 'text' ) {
        parentObj = document.getElementById('teamSelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","team");
        inputSel.setAttribute("id","teamSelect");
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('teamSelect');
        selObj.options[0] = new Option('Select team','');
        selObj.selectedIndex = 0;
      }
      if ( teamCode != '' ) {
        selObj.options[optionCntr] = new Option(teamName, teamCode);
      }
      // See if it's selected from a previous post
      //if ( teamCode == postteam && leagueCode == postleague ) {
	  if (teamCode == defaultTeam) {
        selObj.selectedIndex = optionCntr;
      }
      foundteam = true;
      optionCntr++
    }
  }
  // If the league has no teams, change the select to a text box
  if ( ! foundteam ) {
    parentObj = document.getElementById('teamSelect').parentNode;
    parentObj.removeChild(selObj);
  // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", "teamSelect");
    inputEl.setAttribute("type", "text");
    inputEl.setAttribute("name", "team");
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", postteam);
    parentObj.appendChild(inputEl) ;
  }
}

function initleague(league) {
  populateleague(league);
  populateteam();
}


