	protected Dialog onCreateDialog(final int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
		switch (ChatDialog.values()[id]) {
		case Challenge: {
			if (netServ == null) {
				removeDialog(id);
				dismissDialog(id);
			}
			final IncomingChallenge challenge = netServ.challenges.poll();
			View challengedLayout = inflater.inflate(R.layout.player_info_dialog, (LinearLayout)findViewById(R.id.player_info_dialog));
			PlayerInfo opp = netServ.players.get(challenge.opponent);
			ImageView[] oppPokeIcons = new ImageView[6];
			TextView oppInfo, oppTeam, oppName, oppTier, oppRating;           
			builder.setView(challengedLayout)
			.setCancelable(false)
			.setNegativeButton(this.getString(R.string.decline), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (netServ.socket.isConnected())
						netServ.socket.sendMessage(
								constructChallenge(ChallengeDesc.Refused.ordinal(),
										challenge.opponent,
										challenge.clauses,
										challenge.mode),
										Command.ChallengeStuff);
					removeDialog(id);
					checkChallenges();
				}
			})
			.setPositiveButton(this.getString(R.string.accept), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (netServ.socket.isConnected())
						netServ.socket.sendMessage(
								constructChallenge(ChallengeDesc.Accepted.ordinal(),
										challenge.opponent,
										challenge.clauses,
										challenge.mode),
										Command.ChallengeStuff);
					removeDialog(id);
					checkChallenges();
				}
			});
			final AlertDialog oppInfoDialog = builder.create();
			for(int i = 0; i < 6; i++){
				oppPokeIcons[i] = (ImageView)challengedLayout.findViewById(getResources().getIdentifier("player_info_poke" + 
						(i+1), "id", packName));
			}
			oppInfo = (TextView)challengedLayout.findViewById(getResources().getIdentifier("player_info", "id", packName));
			oppInfo.setText(Html.fromHtml("<b>Info: </b>" + StringUtilities.escapeHtml(opp.info())));
			oppTeam = (TextView)challengedLayout.findViewById(getResources().getIdentifier("player_info_team", "id", packName));
			oppTeam.setText(opp.nick() + "'s team:");
			oppName = (TextView)challengedLayout.findViewById(getResources().getIdentifier("player_info_name", "id", packName));
			oppName.setText(this.getString(R.string.accept_challenge) + " " + opp.nick() + "?");
			oppName.setTextSize(18);
			oppTier = (TextView)challengedLayout.findViewById(getResources().getIdentifier("player_info_tier", "id", packName));
			oppRating = (TextView)challengedLayout.findViewById(getResources().getIdentifier("player_info_rating", "id", packName));
			return oppInfoDialog;
		} case AskForPass: {
        	final EditText passField = new EditText(this);
        	passField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			builder.setMessage("Please enter your password " + netServ.mePlayer.nick() + ".")
			.setCancelable(true)
			.setView(passField)
			.setPositiveButton("Done", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (netServ != null) {
						netServ.sendPass(passField.getText().toString());
					}
					removeDialog(id);
				}
			})
			.setOnCancelListener(new OnCancelListener() {
				public void onCancel(DialogInterface dialog) {
					removeDialog(id);
					disconnect();
				}
			});
			final AlertDialog dialog = builder.create();
        	passField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
				public void onFocusChange(View v, boolean hasFocus) {
					if (hasFocus) {
						dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
					}
				}
			});
			return dialog;
		} case ConfirmDisconnect: {
			builder.setMessage("Really disconnect?")
			.setCancelable(true)
			.setPositiveButton("Disconnect", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					disconnect();
				}
			})
			.setNegativeButton("Cancel", null);
			return builder.create();
		} case FindBattle: {
			final EditText range = new EditText(this);
			range.append("" + (prefs.contains("range") ? prefs.getInt("range", 0) : ""));
			range.setInputType(InputType.TYPE_CLASS_NUMBER);
			range.setHint("Range");
			builder.setTitle(R.string.find_a_battle)
			.setMultiChoiceItems(new CharSequence[]{"Force Rated", "Force Same Tier", "Only within range"}, new boolean[]{prefs.getBoolean("findOption0", false), prefs.getBoolean("findOption1", false), prefs.getBoolean("findOption2", false)}, new DialogInterface.OnMultiChoiceClickListener() {
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					prefs.edit().putBoolean("findOption" + which, isChecked).commit();
				}
			})
			.setView(range)
			.setPositiveButton("Find", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (netServ != null && netServ.socket.isConnected()) {
						netServ.findingBattle = true;
						try {
							prefs.edit().putInt("range", Integer.valueOf(range.getText().toString())).commit();
						} catch (NumberFormatException e) {
							prefs.edit().remove("range").commit();
						}
						netServ.socket.sendMessage(
								constructFindBattle(prefs.getBoolean("findOption0", false), prefs.getBoolean("findOption1", false), prefs.getBoolean("findOption2", false), prefs.getInt("range", 200), (byte) 0),
								Command.FindBattle);
					}
				}
			});
			return builder.create();
		} case TierSelection: {
			if (netServ == null) {
				return null;
			}
			return new TierAlertDialog(this, netServ.superTier);
		} case PlayerInfo: {
			View layout = inflater.inflate(R.layout.player_info_dialog, (LinearLayout)findViewById(R.id.player_info_dialog));
            ImageView[] pPokeIcons = new ImageView[6];
            TextView pInfo, pName;
            ListView ratings;
			builder.setView(layout)
            .setNegativeButton("Back", new DialogInterface.OnClickListener(){
            	public void onClick(DialogInterface dialog, int which) {
            		removeDialog(id);
            	}
            })
            .setOnCancelListener(new DialogInterface.OnCancelListener(){
            	public void onCancel(DialogInterface dialog) {
            		removeDialog(id);
            	}
            })
            .setPositiveButton("Challenge", new DialogInterface.OnClickListener(){
            	public void onClick(DialogInterface dialog, int which) {
        			showDialog(ChatDialog.ChallengeMode.ordinal());
            		removeDialog(id);
            	}});
            final AlertDialog pInfoDialog = builder.create();
            for(int i = 0; i < 6; i++){
        	pPokeIcons[i] = (ImageView)layout.findViewById(getResources().getIdentifier("player_info_poke" + 
        			(i+1), "id", packName));
            }
        	pInfo = (TextView)layout.findViewById(R.id.player_info);
        	pInfo.setText(Html.fromHtml("<b>Info: </b>" + StringUtilities.escapeHtml(lastClickedPlayer.info())));
        	pName = (TextView)layout.findViewById(R.id.player_info_name);
        	pName.setText(lastClickedPlayer.nick());
        	ratings = (ListView)layout.findViewById(R.id.player_info_tiers);
        	ratings.setAdapter(new TwoViewsArrayAdapter<TierStanding>(this, android.R.layout.simple_list_item_2, 
        			android.R.id.text1, android.R.id.text2, lastClickedPlayer.tierStandings, PlayerInfo.tierGetter));
            return pInfoDialog;
		} case ChallengeMode: {
            final Clauses[] clauses = Clauses.values();
            final int numClauses = clauses.length;
			final String[] clauseNames = new String[numClauses];
			final boolean[] checked = new boolean[numClauses];
			for (int i=0; i < numClauses; i++) {
				clauseNames[i] = clauses[i].toString();
				checked[i] = prefs.getBoolean("challengeOption" + i, false);
			}
            builder.setMultiChoiceItems(clauseNames, checked, new DialogInterface.OnMultiChoiceClickListener() {
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					prefs.edit().putBoolean("challengeOption" + which, isChecked).commit();
				}
			})
			.setPositiveButton("Challenge", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					int clauses = 0;
					for (int i = 0; i < numClauses; i++)
						clauses |= (prefs.getBoolean("challengeOption" + i, false) ? Clauses.values()[i].mask() : 0);
					if (netServ != null && netServ.socket != null && netServ.socket.isConnected())
						netServ.socket.sendMessage(constructChallenge(ChallengeDesc.Sent.ordinal(), lastClickedPlayer.id, clauses, Mode.Singles.ordinal()), Command.ChallengeStuff);
					removeDialog(id);
				}
			})
            .setNegativeButton("Back", new DialogInterface.OnClickListener(){
            	public void onClick(DialogInterface dialog, int which) {
            		removeDialog(id);
            	}
            })
            .setOnCancelListener(new DialogInterface.OnCancelListener(){
            	public void onCancel(DialogInterface dialog) {
            		removeDialog(id);
            	}
            })
            .setTitle("Select clauses");
            return builder.create();
		} default: {
			return new Dialog(this);
		}
		}
	}

	protected Dialog onCreateDialog(final int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
		switch (ChatDialog.values()[id]) {
		case Challenge: {
			if (netServ == null) {
				return null;
			}
			final IncomingChallenge challenge = netServ.challenges.poll();
			View challengedLayout = inflater.inflate(R.layout.player_info_dialog, (LinearLayout)findViewById(R.id.player_info_dialog));
			PlayerInfo opp = netServ.players.get(challenge.opponent);
			ImageView[] oppPokeIcons = new ImageView[6];
			TextView oppInfo, oppTeam, oppName, oppTier, oppRating;           
			builder.setView(challengedLayout)
			.setCancelable(false)
			.setNegativeButton(this.getString(R.string.decline), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (netServ.socket.isConnected())
						netServ.socket.sendMessage(
								constructChallenge(ChallengeDesc.Refused.ordinal(),
										challenge.opponent,
										challenge.clauses,
										challenge.mode),
										Command.ChallengeStuff);
					removeDialog(id);
					checkChallenges();
				}
			})
			.setPositiveButton(this.getString(R.string.accept), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (netServ.socket.isConnected())
						netServ.socket.sendMessage(
								constructChallenge(ChallengeDesc.Accepted.ordinal(),
										challenge.opponent,
										challenge.clauses,
										challenge.mode),
										Command.ChallengeStuff);
					removeDialog(id);
					checkChallenges();
				}
			});
			final AlertDialog oppInfoDialog = builder.create();
			for(int i = 0; i < 6; i++){
				oppPokeIcons[i] = (ImageView)challengedLayout.findViewById(getResources().getIdentifier("player_info_poke" + 
						(i+1), "id", packName));
			}
			oppInfo = (TextView)challengedLayout.findViewById(getResources().getIdentifier("player_info", "id", packName));
			oppInfo.setText(Html.fromHtml("<b>Info: </b>" + StringUtilities.escapeHtml(opp.info())));
			oppTeam = (TextView)challengedLayout.findViewById(getResources().getIdentifier("player_info_team", "id", packName));
			oppTeam.setText(opp.nick() + "'s team:");
			oppName = (TextView)challengedLayout.findViewById(getResources().getIdentifier("player_info_name", "id", packName));
			oppName.setText(this.getString(R.string.accept_challenge) + " " + opp.nick() + "?");
			oppName.setTextSize(18);
			oppTier = (TextView)challengedLayout.findViewById(getResources().getIdentifier("player_info_tier", "id", packName));
			oppRating = (TextView)challengedLayout.findViewById(getResources().getIdentifier("player_info_rating", "id", packName));
			return oppInfoDialog;
		} case AskForPass: {
        	final EditText passField = new EditText(this);
        	passField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			builder.setMessage("Please enter your password " + netServ.mePlayer.nick() + ".")
			.setCancelable(true)
			.setView(passField)
			.setPositiveButton("Done", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (netServ != null) {
						netServ.sendPass(passField.getText().toString());
					}
					removeDialog(id);
				}
			})
			.setOnCancelListener(new OnCancelListener() {
				public void onCancel(DialogInterface dialog) {
					removeDialog(id);
					disconnect();
				}
			});
			final AlertDialog dialog = builder.create();
        	passField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
				public void onFocusChange(View v, boolean hasFocus) {
					if (hasFocus) {
						dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
					}
				}
			});
			return dialog;
		} case ConfirmDisconnect: {
			builder.setMessage("Really disconnect?")
			.setCancelable(true)
			.setPositiveButton("Disconnect", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					disconnect();
				}
			})
			.setNegativeButton("Cancel", null);
			return builder.create();
		} case FindBattle: {
			final EditText range = new EditText(this);
			range.append("" + (prefs.contains("range") ? prefs.getInt("range", 0) : ""));
			range.setInputType(InputType.TYPE_CLASS_NUMBER);
			range.setHint("Range");
			builder.setTitle(R.string.find_a_battle)
			.setMultiChoiceItems(new CharSequence[]{"Force Rated", "Force Same Tier", "Only within range"}, new boolean[]{prefs.getBoolean("findOption0", false), prefs.getBoolean("findOption1", false), prefs.getBoolean("findOption2", false)}, new DialogInterface.OnMultiChoiceClickListener() {
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					prefs.edit().putBoolean("findOption" + which, isChecked).commit();
				}
			})
			.setView(range)
			.setPositiveButton("Find", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (netServ != null && netServ.socket.isConnected()) {
						netServ.findingBattle = true;
						try {
							prefs.edit().putInt("range", Integer.valueOf(range.getText().toString())).commit();
						} catch (NumberFormatException e) {
							prefs.edit().remove("range").commit();
						}
						netServ.socket.sendMessage(
								constructFindBattle(prefs.getBoolean("findOption0", false), prefs.getBoolean("findOption1", false), prefs.getBoolean("findOption2", false), prefs.getInt("range", 200), (byte) 0),
								Command.FindBattle);
					}
				}
			});
			return builder.create();
		} case TierSelection: {
			if (netServ == null) {
				return null;
			}
			return new TierAlertDialog(this, netServ.superTier);
		} case PlayerInfo: {
			View layout = inflater.inflate(R.layout.player_info_dialog, (LinearLayout)findViewById(R.id.player_info_dialog));
            ImageView[] pPokeIcons = new ImageView[6];
            TextView pInfo, pName;
            ListView ratings;
			builder.setView(layout)
            .setNegativeButton("Back", new DialogInterface.OnClickListener(){
            	public void onClick(DialogInterface dialog, int which) {
            		removeDialog(id);
            	}
            })
            .setOnCancelListener(new DialogInterface.OnCancelListener(){
            	public void onCancel(DialogInterface dialog) {
            		removeDialog(id);
            	}
            })
            .setPositiveButton("Challenge", new DialogInterface.OnClickListener(){
            	public void onClick(DialogInterface dialog, int which) {
        			showDialog(ChatDialog.ChallengeMode.ordinal());
            		removeDialog(id);
            	}});
            final AlertDialog pInfoDialog = builder.create();
            for(int i = 0; i < 6; i++){
        	pPokeIcons[i] = (ImageView)layout.findViewById(getResources().getIdentifier("player_info_poke" + 
        			(i+1), "id", packName));
            }
        	pInfo = (TextView)layout.findViewById(R.id.player_info);
        	pInfo.setText(Html.fromHtml("<b>Info: </b>" + StringUtilities.escapeHtml(lastClickedPlayer.info())));
        	pName = (TextView)layout.findViewById(R.id.player_info_name);
        	pName.setText(lastClickedPlayer.nick());
        	ratings = (ListView)layout.findViewById(R.id.player_info_tiers);
        	ratings.setAdapter(new TwoViewsArrayAdapter<TierStanding>(this, android.R.layout.simple_list_item_2, 
        			android.R.id.text1, android.R.id.text2, lastClickedPlayer.tierStandings, PlayerInfo.tierGetter));
            return pInfoDialog;
		} case ChallengeMode: {
            final Clauses[] clauses = Clauses.values();
            final int numClauses = clauses.length;
			final String[] clauseNames = new String[numClauses];
			final boolean[] checked = new boolean[numClauses];
			for (int i=0; i < numClauses; i++) {
				clauseNames[i] = clauses[i].toString();
				checked[i] = prefs.getBoolean("challengeOption" + i, false);
			}
            builder.setMultiChoiceItems(clauseNames, checked, new DialogInterface.OnMultiChoiceClickListener() {
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					prefs.edit().putBoolean("challengeOption" + which, isChecked).commit();
				}
			})
			.setPositiveButton("Challenge", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					int clauses = 0;
					for (int i = 0; i < numClauses; i++)
						clauses |= (prefs.getBoolean("challengeOption" + i, false) ? Clauses.values()[i].mask() : 0);
					if (netServ != null && netServ.socket != null && netServ.socket.isConnected())
						netServ.socket.sendMessage(constructChallenge(ChallengeDesc.Sent.ordinal(), lastClickedPlayer.id, clauses, Mode.Singles.ordinal()), Command.ChallengeStuff);
					removeDialog(id);
				}
			})
            .setNegativeButton("Back", new DialogInterface.OnClickListener(){
            	public void onClick(DialogInterface dialog, int which) {
            		removeDialog(id);
            	}
            })
            .setOnCancelListener(new DialogInterface.OnCancelListener(){
            	public void onCancel(DialogInterface dialog) {
            		removeDialog(id);
            	}
            })
            .setTitle("Select clauses");
            return builder.create();
		} default: {
			return new Dialog(this);
		}
		}
	}
