text: function Game() {
  	var die1;
  	var die2;
  	var areDiceRolled = false;
  
  	var auctionQueue = [];
  	var highestbidder;
  	var highestbid;
  	var currentbidder = 1;
  	var auctionproperty;
  
  	this.rollDice = function() {
  		die1 = Math.floor(Math.random() * 6) + 1;
  		die2 = Math.floor(Math.random() * 6) + 1;
  		areDiceRolled = true;
  	};
  
  	this.resetDice = function() {
  		areDiceRolled = false;
  	};
  
  	this.next = function() {
  		if (!p.human && p.money < 0) {
  			p.AI.payDebt();
  
  			if (p.money < 0) {
  				popup("<p>" + p.name + " is bankrupt. All of its assets will be turned over to " + player[p.creditor].name + ".</p>", game.bankruptcy);
  			} else {
  				roll();
  			}
  		} else if (areDiceRolled && doublecount === 0) {
  			var s = square[p.position];
  			if (document.getElementById("question-answer").value == s.answer) {
  				play();
  			} else {
  				addAlert("That's the wrong answer")
  			}
  		} else {
  			roll();
  		}
  	};
  
  	this.getDie = function(die) {
  		if (die === 1) {
  
  			return die1;
  		} else {
  
  			return die2;
  		}
  
  	};
  
  
  
  	// Auction functions:
  
  
  
  	var finalizeAuction = function() {
  		var p = player[highestbidder];
  		var sq = square[auctionproperty];
  
  		if (highestbid > 0) {
  			p.pay(highestbid, 0);
  			sq.owner = highestbidder;
  			addAlert(p.name + " bought " + sq.name + " for $" + highestbid + ".");
  		}
  
  		for (var i = 1; i <= pcount; i++) {
  			player[i].bidding = true;
  		}
  
  		$("#popupbackground").hide();
  		$("#popupwrap").hide();
  
  		if (!game.auction()) {
  			play();
  		}
  	};
  
  	this.addPropertyToAuctionQueue = function(propertyIndex) {
  		auctionQueue.push(propertyIndex);
  	};
  
  	this.auction = function() {
  		if (auctionQueue.length === 0) {
  			return false;
  		}
  
  		index = auctionQueue.shift();
  
  		var s = square[index];
  
  		if (s.price === 0 || s.owner !== 0) {
  			return game.auction();
  		}
  
  		auctionproperty = index;
  		highestbidder = 0;
  		highestbid = 0;
  		currentbidder = turn + 1;
  
  		if (currentbidder > pcount) {
  			currentbidder -= pcount;
  		}
  
  		popup("<div style='font-weight: bold; font-size: 16px; margin-bottom: 10px;'>Auction <span id='propertyname'></span></div><div>Highest Bid = $<span id='highestbid'></span> (<span id='highestbidder'></span>)</div><div><span id='currentbidder'></span>, it is your turn to bid.</div<div><input id='bid' title='Enter an amount to bid on " + s.name + ".' style='width: 291px;' /></div><div><input type='button' value='Bid' onclick='game.auctionBid();' title='Place your bid.' /><input type='button' value='Pass' title='Skip bidding this time.' onclick='game.auctionPass();' /><input type='button' value='Exit Auction' title='Stop bidding on " + s.name + " altogether.' onclick='if (confirm(\"Are you sure you want to stop bidding on this property altogether?\")) game.auctionExit();' /></div>", "blank");
  
  		document.getElementById("propertyname").innerHTML = "<a href='javascript:void(0);' onmouseover='showdeed(" + auctionproperty + ");' onmouseout='hidedeed();' class='statscellcolor'>" + s.name + "</a>";
  		document.getElementById("highestbid").innerHTML = "0";
  		document.getElementById("highestbidder").innerHTML = "N/A";
  		document.getElementById("currentbidder").innerHTML = player[currentbidder].name;
  		document.getElementById("bid").onkeydown = function (e) {
  			var key = 0;
  			var isCtrl = false;
  			var isShift = false;
  
  			if (window.event) {
  				key = window.event.keyCode;
  				isCtrl = window.event.ctrlKey;
  				isShift = window.event.shiftKey;
  			} else if (e) {
  				key = e.keyCode;
  				isCtrl = e.ctrlKey;
  				isShift = e.shiftKey;
  			}
  
  			if (isNaN(key)) {
  				return true;
  			}
  
  			if (key === 13) {
  				game.auctionBid();
  				return false;
  			}
  
  			// Allow backspace, tab, delete, arrow keys, or if control was pressed, respectively.
  			if (key === 8 || key === 9 || key === 46 || (key >= 35 && key <= 40) || isCtrl) {
  				return true;
  			}
  
  			if (isShift) {
  				return false;
  			}
  
  			// Only allow number keys.
  			return (key >= 48 && key <= 57) || (key >= 96 && key <= 105);
  		};
  
  		document.getElementById("bid").onfocus = function () {
  			this.style.color = "black";
  			if (isNaN(this.value)) {
  				this.value = "";
  			}
  		};
  
  		updateMoney();
  
  		if (!player[currentbidder].human) {
  			currentbidder = turn; // auctionPass advances currentbidder.
  			this.auctionPass();
  		}
  		return true;
  	};
  
  	this.auctionPass = function() {
  		if (highestbidder === 0) {
  			highestbidder = currentbidder;
  		}
  
  		while (true) {
  			currentbidder++;
  
  			if (currentbidder > pcount) {
  				currentbidder -= pcount;
  			}
  
  			if (currentbidder == highestbidder) {
  				finalizeAuction();
  				return;
  			} else if (player[currentbidder].bidding) {
  				var p = player[currentbidder];
  
  				if (!p.human) {
  					var bid = p.AI.bid(auctionproperty, highestbid);
  
  					if (bid === -1 || highestbid >= p.money) {
  						p.bidding = false;
  
  						window.alert(p.name + " exited the auction.");
  						continue;
  
  					} else if (bid === 0) {
  						window.alert(p.name + " passed.");
  						continue;
  
  					} else if (bid > 0) {
  						this.auctionBid(bid);
  						window.alert(p.name + " bid $" + bid + ".");
  						continue;
  					}
  					return;
  				} else {
  					break;
  				}
  			}
  
  		}
  
  		document.getElementById("currentbidder").innerHTML = player[currentbidder].name;
  		document.getElementById("bid").value = "";
  		document.getElementById("bid").style.color = "black";
  	};
  
  	this.auctionBid = function(bid) {
  		bid = bid || parseInt(document.getElementById("bid").value, 10);
  
  		if (bid === "" || bid === null) {
  			document.getElementById("bid").value = "Please enter a bid.";
  			document.getElementById("bid").style.color = "red";
  		} else if (isNaN(bid)) {
  			document.getElementById("bid").value = "Your bid must be a number.";
  			document.getElementById("bid").style.color = "red";
  		} else {
  
  			if (bid > player[currentbidder].money) {
  				document.getElementById("bid").value = "You don't have enough money to bid $" + bid + ".";
  				document.getElementById("bid").style.color = "red";
  			} else if (bid > highestbid) {
  				highestbid = bid;
  				document.getElementById("highestbid").innerHTML = parseInt(bid, 10);
  				highestbidder = currentbidder;
  				document.getElementById("highestbidder").innerHTML = player[highestbidder].name;
  
  				document.getElementById("bid").focus();
  
  				if (player[currentbidder].human) {
  					this.auctionPass();
  				}
  			} else {
  				document.getElementById("bid").value = "Your bid must be greater than highest bid. ($" + highestbid + ")";
  				document.getElementById("bid").style.color = "red";
  			}
  		}
  	};
  
  	this.auctionExit = function() {
  		player[currentbidder].bidding = false;
  		this.auctionPass();
  	};
  
  
  
  	// Trade functions:
  
  
  
  	var currentInitiator;
  	var currentRecipient;
  
  	// Define event handlers:
  
  	var tradeMoneyOnKeyDown = function (e) {
  		var key = 0;
  		var isCtrl = false;
  		var isShift = false;
  
  		if (window.event) {
  			key = window.event.keyCode;
  			isCtrl = window.event.ctrlKey;
  			isShift = window.event.shiftKey;
  		} else if (e) {
  			key = e.keyCode;
  			isCtrl = e.ctrlKey;
  			isShift = e.shiftKey;
  		}
  
  		if (isNaN(key)) {
  			return true;
  		}
  
  		if (key === 13) {
  			return false;
  		}
  
  		// Allow backspace, tab, delete, arrow keys, or if control was pressed, respectively.
  		if (key === 8 || key === 9 || key === 46 || (key >= 35 && key <= 40) || isCtrl) {
  			return true;
  		}
  
  		if (isShift) {
  			return false;
  		}
  
  		// Only allow number keys.
  		return (key >= 48 && key <= 57) || (key >= 96 && key <= 105);
  	};
  
  	var tradeMoneyOnFocus = function () {
  		this.style.color = "black";
  		if (isNaN(this.value) || this.value === "0") {
  			this.value = "";
  		}
  	};
  
  	var tradeMoneyOnChange = function(e) {
  		$("#proposetradebutton").show();
  		$("#canceltradebutton").show();
  		$("#accepttradebutton").hide();
  		$("#rejecttradebutton").hide();
  
  		var amount = this.value;
  
  		if (isNaN(amount)) {
  			this.value = "This value must be a number.";
  			this.style.color = "red";
  			return false;
  		}
  
  		amount = Math.round(amount) || 0;
  		this.value = amount;
  
  		if (amount < 0) {
  			this.value = "This value must be greater than 0.";
  			this.style.color = "red";
  			return false;
  		}
  
  		return true;
  	};
  
  	document.getElementById("trade-leftp-money").onkeydown = tradeMoneyOnKeyDown;
  	document.getElementById("trade-rightp-money").onkeydown = tradeMoneyOnKeyDown;
  	document.getElementById("trade-leftp-money").onfocus = tradeMoneyOnFocus;
  	document.getElementById("trade-rightp-money").onfocus = tradeMoneyOnFocus;
  	document.getElementById("trade-leftp-money").onchange = tradeMoneyOnChange;
  	document.getElementById("trade-rightp-money").onchange = tradeMoneyOnChange;
  
  	var resetTrade = function(initiator, recipient, allowRecipientToBeChanged) {
  		var currentSquare;
  		var currentTableRow;
  		var currentTableCell;
  		var currentTableCellCheckbox;
  		var nameSelect;
  		var currentOption;
  		var allGroupUninproved;
  		var currentName;
  
  		var tableRowOnClick = function(e) {
  			var checkboxElement = this.firstChild.firstChild;
  
  			if (checkboxElement !== e.srcElement) {
  				checkboxElement.checked = !checkboxElement.checked;
  			}
  
  			$("#proposetradebutton").show();
  			$("#canceltradebutton").show();
  			$("#accepttradebutton").hide();
  			$("#rejecttradebutton").hide();
  		};
  
  		var initiatorProperty = document.getElementById("trade-leftp-property");
  		var recipientProperty = document.getElementById("trade-rightp-property");
  
  		currentInitiator = initiator;
  		currentRecipient = recipient;
  
  		// Empty elements.
  		while (initiatorProperty.lastChild) {
  			initiatorProperty.removeChild(initiatorProperty.lastChild);
  		}
  
  		while (recipientProperty.lastChild) {
  			recipientProperty.removeChild(recipientProperty.lastChild);
  		}
  
  		var initiatorSideTable = document.createElement("table");
  		var recipientSideTable = document.createElement("table");
  
  
  		for (var i = 0; i < 40; i++) {
  			currentSquare = square[i];
  
  			// A property cannot be traded if any properties in its group have been improved.
  			if (currentSquare.house > 0 || currentSquare.groupNumber === 0) {
  				continue;
  			}
  
  			allGroupUninproved = true;
  			var max = currentSquare.group.length;
  			for (var j = 0; j < max; j++) {
  
  				if (square[currentSquare.group[j]].house > 0) {
  					allGroupUninproved = false;
  					break;
  				}
  			}
  
  			if (!allGroupUninproved) {
  				continue;
  			}
  
  			// Offered properties.
  			if (currentSquare.owner === initiator.index) {
  				currentTableRow = initiatorSideTable.appendChild(document.createElement("tr"));
  				currentTableRow.onclick = tableRowOnClick;
  
  				currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  				currentTableCell.className = "propertycellcheckbox";
  				currentTableCellCheckbox = currentTableCell.appendChild(document.createElement("input"));
  				currentTableCellCheckbox.type = "checkbox";
  				currentTableCellCheckbox.id = "tradeleftcheckbox" + i;
  				currentTableCellCheckbox.title = "Check this box to include " + currentSquare.name + " in the trade.";
  
  				currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  				currentTableCell.className = "propertycellcolor";
  				currentTableCell.style.backgroundColor = currentSquare.color;
  
  				if (currentSquare.groupNumber == 1 || currentSquare.groupNumber == 2) {
  					currentTableCell.style.borderColor = "grey";
  				} else {
  					currentTableCell.style.borderColor = currentSquare.color;
  				}
  
  				currentTableCell.propertyIndex = i;
  				currentTableCell.onmouseover = function() {showdeed(this.propertyIndex);};
  				currentTableCell.onmouseout = hidedeed;
  
  				currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  				currentTableCell.className = "propertycellname";
  				if (currentSquare.mortgage) {
  					currentTableCell.title = "Mortgaged";
  					currentTableCell.style.color = "grey";
  				}
  				currentTableCell.textContent = currentSquare.name;
  
  			// Requested properties.
  			} else if (currentSquare.owner === recipient.index) {
  				currentTableRow = recipientSideTable.appendChild(document.createElement("tr"));
  				currentTableRow.onclick = tableRowOnClick;
  
  				currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  				currentTableCell.className = "propertycellcheckbox";
  				currentTableCellCheckbox = currentTableCell.appendChild(document.createElement("input"));
  				currentTableCellCheckbox.type = "checkbox";
  				currentTableCellCheckbox.id = "traderightcheckbox" + i;
  				currentTableCellCheckbox.title = "Check this box to include " + currentSquare.name + " in the trade.";
  
  				currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  				currentTableCell.className = "propertycellcolor";
  				currentTableCell.style.backgroundColor = currentSquare.color;
  
  				if (currentSquare.groupNumber == 1 || currentSquare.groupNumber == 2) {
  					currentTableCell.style.borderColor = "grey";
  				} else {
  					currentTableCell.style.borderColor = currentSquare.color;
  				}
  
  				currentTableCell.propertyIndex = i;
  				currentTableCell.onmouseover = function() {showdeed(this.propertyIndex);};
  				currentTableCell.onmouseout = hidedeed;
  
  				currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  				currentTableCell.className = "propertycellname";
  				if (currentSquare.mortgage) {
  					currentTableCell.title = "Mortgaged";
  					currentTableCell.style.color = "grey";
  				}
  				currentTableCell.textContent = currentSquare.name;
  			}
  		}
  
  		if (initiator.communityChestJailCard) {
  			currentTableRow = initiatorSideTable.appendChild(document.createElement("tr"));
  			currentTableRow.onclick = tableRowOnClick;
  
  			currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  			currentTableCell.className = "propertycellcheckbox";
  			currentTableCellCheckbox = currentTableCell.appendChild(document.createElement("input"));
  			currentTableCellCheckbox.type = "checkbox";
  			currentTableCellCheckbox.id = "tradeleftcheckbox40";
  			currentTableCellCheckbox.title = "Check this box to include this Get Out of Jail Free Card in the trade.";
  
  			currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  			currentTableCell.className = "propertycellcolor";
  			currentTableCell.style.backgroundColor = "white";
  			currentTableCell.style.borderColor = "grey";
  
  			currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  			currentTableCell.className = "propertycellname";
  
  			currentTableCell.textContent = "Get Out of Jail Free Card";
  		} else if (recipient.communityChestJailCard) {
  			currentTableRow = recipientSideTable.appendChild(document.createElement("tr"));
  			currentTableRow.onclick = tableRowOnClick;
  
  			currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  			currentTableCell.className = "propertycellcheckbox";
  			currentTableCellCheckbox = currentTableCell.appendChild(document.createElement("input"));
  			currentTableCellCheckbox.type = "checkbox";
  			currentTableCellCheckbox.id = "traderightcheckbox40";
  			currentTableCellCheckbox.title = "Check this box to include this Get Out of Jail Free Card in the trade.";
  
  			currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  			currentTableCell.className = "propertycellcolor";
  			currentTableCell.style.backgroundColor = "white";
  			currentTableCell.style.borderColor = "grey";
  
  			currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  			currentTableCell.className = "propertycellname";
  
  			currentTableCell.textContent = "Get Out of Jail Free Card";
  		}
  
  		if (initiator.chanceJailCard) {
  			currentTableRow = initiatorSideTable.appendChild(document.createElement("tr"));
  			currentTableRow.onclick = tableRowOnClick;
  
  			currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  			currentTableCell.className = "propertycellcheckbox";
  			currentTableCellCheckbox = currentTableCell.appendChild(document.createElement("input"));
  			currentTableCellCheckbox.type = "checkbox";
  			currentTableCellCheckbox.id = "tradeleftcheckbox41";
  			currentTableCellCheckbox.title = "Check this box to include this Get Out of Jail Free Card in the trade.";
  
  			currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  			currentTableCell.className = "propertycellcolor";
  			currentTableCell.style.backgroundColor = "white";
  			currentTableCell.style.borderColor = "grey";
  
  			currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  			currentTableCell.className = "propertycellname";
  
  			currentTableCell.textContent = "Get Out of Jail Free Card";
  		} else if (recipient.chanceJailCard) {
  			currentTableRow = recipientSideTable.appendChild(document.createElement("tr"));
  			currentTableRow.onclick = tableRowOnClick;
  
  			currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  			currentTableCell.className = "propertycellcheckbox";
  			currentTableCellCheckbox = currentTableCell.appendChild(document.createElement("input"));
  			currentTableCellCheckbox.type = "checkbox";
  			currentTableCellCheckbox.id = "traderightcheckbox41";
  			currentTableCellCheckbox.title = "Check this box to include this Get Out of Jail Free Card in the trade.";
  
  			currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  			currentTableCell.className = "propertycellcolor";
  			currentTableCell.style.backgroundColor = "white";
  			currentTableCell.style.borderColor = "grey";
  
  			currentTableCell = currentTableRow.appendChild(document.createElement("td"));
  			currentTableCell.className = "propertycellname";
  
  			currentTableCell.textContent = "Get Out of Jail Free Card";
  		}
  
  		if (initiatorSideTable.lastChild) {
  			initiatorProperty.appendChild(initiatorSideTable);
  		} else {
  			initiatorProperty.textContent = initiator.name + " has no properties to trade.";
  		}
  
  		if (recipientSideTable.lastChild) {
  			recipientProperty.appendChild(recipientSideTable);
  		} else {
  			recipientProperty.textContent = recipient.name + " has no properties to trade.";
  		}
  
  		document.getElementById("trade-leftp-name").textContent = initiator.name;
  
  		currentName = document.getElementById("trade-rightp-name");
  
  		if (allowRecipientToBeChanged && pcount > 2) {
  			// Empty element.
  			while (currentName.lastChild) {
  				currentName.removeChild(currentName.lastChild);
  			}
  
  			nameSelect = currentName.appendChild(document.createElement("select"));
  			for (var i = 1; i <= pcount; i++) {
  				if (i === initiator.index) {
  					continue;
  				}
  
  				currentOption = nameSelect.appendChild(document.createElement("option"));
  				currentOption.value = i + "";
  				currentOption.style.color = player[i].color;
  				currentOption.textContent = player[i].name;
  
  				if (i === recipient.index) {
  					currentOption.selected = "selected";
  				}
  			}
  
  			nameSelect.onchange = function() {
  				resetTrade(currentInitiator, player[parseInt(this.value, 10)], true);
  			};
  
  			nameSelect.title = "Select a player to trade with.";
  		} else {
  			currentName.textContent = recipient.name;
  		}
  
  		document.getElementById("trade-leftp-money").value = "0";
  		document.getElementById("trade-rightp-money").value = "0";
  
  	};
  
  	var readTrade = function() {
  		var initiator = currentInitiator;
  		var recipient = currentRecipient;
  		var property = new Array(40);
  		var money;
  		var communityChestJailCard;
  		var chanceJailCard;
  
  		for (var i = 0; i < 40; i++) {
  
  			if (document.getElementById("tradeleftcheckbox" + i) && document.getElementById("tradeleftcheckbox" + i).checked) {
  				property[i] = 1;
  			} else if (document.getElementById("traderightcheckbox" + i) && document.getElementById("traderightcheckbox" + i).checked) {
  				property[i] = -1;
  			} else {
  				property[i] = 0;
  			}
  		}
  
  		if (document.getElementById("tradeleftcheckbox40") && document.getElementById("tradeleftcheckbox40").checked) {
  			communityChestJailCard = 1;
  		} else if (document.getElementById("traderightcheckbox40") && document.getElementById("traderightcheckbox40").checked) {
  			communityChestJailCard = -1;
  		} else {
  			communityChestJailCard = 0;
  		}
  
  		if (document.getElementById("tradeleftcheckbox41") && document.getElementById("tradeleftcheckbox41").checked) {
  			chanceJailCard = 1;
  		} else if (document.getElementById("traderightcheckbox41") && document.getElementById("traderightcheckbox41").checked) {
  			chanceJailCard = -1;
  		} else {
  			chanceJailCard = 0;
  		}
  
  		money = parseInt(document.getElementById("trade-leftp-money").value, 10) || 0;
  		money -= parseInt(document.getElementById("trade-rightp-money").value, 10) || 0;
  
  		var trade = new Trade(initiator, recipient, money, property, communityChestJailCard, chanceJailCard);
  
  		return trade;
  	};
  
  	var writeTrade = function(tradeObj) {
  		resetTrade(tradeObj.getInitiator(), tradeObj.getRecipient(), false);
  
  		for (var i = 0; i < 40; i++) {
  
  			if (document.getElementById("tradeleftcheckbox" + i)) {
  				document.getElementById("tradeleftcheckbox" + i).checked = false;
  				if (tradeObj.getProperty(i) === 1) {
  					document.getElementById("tradeleftcheckbox" + i).checked = true;
  				}
  			}
  
  			if (document.getElementById("traderightcheckbox" + i)) {
  				document.getElementById("traderightcheckbox" + i).checked = false;
  				if (tradeObj.getProperty(i) === -1) {
  					document.getElementById("traderightcheckbox" + i).checked = true;
  				}
  			}
  		}
  
  		if (document.getElementById("tradeleftcheckbox40")) {
  			if (tradeObj.getCommunityChestJailCard() === 1) {
  				document.getElementById("tradeleftcheckbox40").checked = true;
  			} else {
  				document.getElementById("tradeleftcheckbox40").checked = false;
  			}
  		}
  
  		if (document.getElementById("traderightcheckbox40")) {
  			if (tradeObj.getCommunityChestJailCard() === -1) {
  				document.getElementById("traderightcheckbox40").checked = true;
  			} else {
  				document.getElementById("traderightcheckbox40").checked = false;
  			}
  		}
  
  		if (document.getElementById("tradeleftcheckbox41")) {
  			if (tradeObj.getChanceJailCard() === 1) {
  				document.getElementById("tradeleftcheckbox41").checked = true;
  			} else {
  				document.getElementById("tradeleftcheckbox41").checked = false;
  			}
  		}
  
  		if (document.getElementById("traderightcheckbox41")) {
  			if (tradeObj.getChanceJailCard() === -1) {
  				document.getElementById("traderightcheckbox41").checked = true;
  			} else {
  				document.getElementById("traderightcheckbox41").checked = false;
  			}
  		}
  
  		if (tradeObj.getMoney() > 0) {
  			document.getElementById("trade-leftp-money").value = tradeObj.getMoney() + "";
  		} else {
  			document.getElementById("trade-rightp-money").value = (-tradeObj.getMoney()) + "";
  		}
  
  	};
  
  	this.trade = function(tradeObj) {
  		$("#board").hide();
  		$("#control").hide();
  		$("#trade").show();
  		$("#proposetradebutton").show();
  		$("#canceltradebutton").show();
  		$("#accepttradebutton").hide();
  		$("#rejecttradebutton").hide();
  
  		if (tradeObj instanceof Trade) {
  			writeTrade(tradeObj);
  			this.proposeTrade();
  		} else {
  			var initiator = player[turn];
  			var recipient = turn === 1 ? player[2] : player[1];
  
  			currentInitiator = initiator;
  			currentRecipient = recipient;
  
  			resetTrade(initiator, recipient, true);
  		}
  	};
  
  
  	this.cancelTrade = function() {
  		$("#board").show();
  		$("#control").show();
  		$("#trade").hide();
  
  
  		if (!player[turn].human) {
  			player[turn].AI.alertList = "";
  			game.next();
  		}
  
  	};
  
  	this.acceptTrade = function(tradeObj) {
  		if (isNaN(document.getElementById("trade-leftp-money").value)) {
  			document.getElementById("trade-leftp-money").value = "This value must be a number.";
  			document.getElementById("trade-leftp-money").style.color = "red";
  			return false;
  		}
  
  		if (isNaN(document.getElementById("trade-rightp-money").value)) {
  			document.getElementById("trade-rightp-money").value = "This value must be a number.";
  			document.getElementById("trade-rightp-money").style.color = "red";
  			return false;
  		}
  
  		var showAlerts = true;
  		var money;
  		var initiator;
  		var recipient;
  
  		if (tradeObj) {
  			showAlerts = false;
  		} else {
  			tradeObj = readTrade();
  		}
  
  		money = tradeObj.getMoney();
  		initiator = tradeObj.getInitiator();
  		recipient = tradeObj.getRecipient();
  
  
  		if (money > 0 && money > initiator.money) {
  			document.getElementById("trade-leftp-money").value = initiator.name + " does not have $" + money + ".";
  			document.getElementById("trade-leftp-money").style.color = "red";
  			return false;
  		} else if (money < 0 && -money > recipient.money) {
  			document.getElementById("trade-rightp-money").value = recipient.name + " does not have $" + (-money) + ".";
  			document.getElementById("trade-rightp-money").style.color = "red";
  			return false;
  		}
  
  		var isAPropertySelected = 0;
  
  		// Ensure that some properties are selected.
  		for (var i = 0; i < 40; i++) {
  			isAPropertySelected |= tradeObj.getProperty(i);
  		}
  
  		isAPropertySelected |= tradeObj.getCommunityChestJailCard();
  		isAPropertySelected |= tradeObj.getChanceJailCard();
  
  		if (isAPropertySelected === 0) {
  			popup("<p>One or more properties must be selected in order to trade.</p>");
  
  			return false;
  		}
  
  		if (showAlerts && !confirm(initiator.name + ", are you sure you want to make this exchange with " + recipient.name + "?")) {
  			return false;
  		}
  
  		// Exchange properties
  		for (var i = 0; i < 40; i++) {
  
  			if (tradeObj.getProperty(i) === 1) {
  				square[i].owner = recipient.index;
  				addAlert(recipient.name + " received " + square[i].name + " from " + initiator.name + ".");
  			} else if (tradeObj.getProperty(i) === -1) {
  				square[i].owner = initiator.index;
  				addAlert(initiator.name + " received " + square[i].name + " from " + recipient.name + ".");
  			}
  
  		}
  
  		if (tradeObj.getCommunityChestJailCard() === 1) {
  			initiator.communityChestJailCard = false;
  			recipient.communityChestJailCard = true;
  			addAlert(recipient.name + ' received a "Get Out of Jail Free" card from ' + initiator.name + ".");
  		} else if (tradeObj.getCommunityChestJailCard() === -1) {
  			initiator.communityChestJailCard = true;
  			recipient.communityChestJailCard = false;
  			addAlert(initiator.name + ' received a "Get Out of Jail Free" card from ' + recipient.name + ".");
  		}
  
  		if (tradeObj.getChanceJailCard() === 1) {
  			initiator.chanceJailCard = false;
  			recipient.chanceJailCard = true;
  			addAlert(recipient.name + ' received a "Get Out of Jail Free" card from ' + initiator.name + ".");
  		} else if (tradeObj.getChanceJailCard() === -1) {
  			initiator.chanceJailCard = true;
  			recipient.chanceJailCard = false;
  			addAlert(initiator.name + ' received a "Get Out of Jail Free" card from ' + recipient.name + ".");
  		}
  
  		// Exchange money.
  		if (money > 0) {
  			initiator.pay(money, recipient.index);
  			recipient.money += money;
  
  			addAlert(recipient.name + " received $" + money + " from " + initiator.name + ".");
  		} else if (money < 0) {
  			money = -money;
  
  			recipient.pay(money, initiator.index);
  			initiator.money += money;
  
  			addAlert(initiator.name + " received $" + money + " from " + recipient.name + ".");
  		}
  
  		updateOwned();
  		updateMoney();
  
  		$("#board").show();
  		$("#control").show();
  		$("#trade").hide();
  
  		if (!player[turn].human) {
  			player[turn].AI.alertList = "";
  			game.next();
  		}
  	};
  
  	this.proposeTrade = function() {
  		if (isNaN(document.getElementById("trade-leftp-money").value)) {
  			document.getElementById("trade-leftp-money").value = "This value must be a number.";
  			document.getElementById("trade-leftp-money").style.color = "red";
  			return false;
  		}
  
  		if (isNaN(document.getElementById("trade-rightp-money").value)) {
  			document.getElementById("trade-rightp-money").value = "This value must be a number.";
  			document.getElementById("trade-rightp-money").style.color = "red";
  			return false;
  		}
  
  		var tradeObj = readTrade();
  		var money = tradeObj.getMoney();
  		var initiator = tradeObj.getInitiator();
  		var recipient = tradeObj.getRecipient();
  		var reversedTradeProperty = [];
  
  		if (money > 0 && money > initiator.money) {
  			document.getElementById("trade-leftp-money").value = initiator.name + " does not have $" + money + ".";
  			document.getElementById("trade-leftp-money").style.color = "red";
  			return false;
  		} else if (money < 0 && -money > recipient.money) {
  			document.getElementById("trade-rightp-money").value = recipient.name + " does not have $" + (-money) + ".";
  			document.getElementById("trade-rightp-money").style.color = "red";
  			return false;
  		}
  
  		var isAPropertySelected = 0;
  
  		// Ensure that some properties are selected.
  		for (var i = 0; i < 40; i++) {
  			reversedTradeProperty[i] = -tradeObj.getProperty(i);
  			isAPropertySelected |= tradeObj.getProperty(i);
  		}
  
  		isAPropertySelected |= tradeObj.getCommunityChestJailCard();
  		isAPropertySelected |= tradeObj.getChanceJailCard();
  
  		if (isAPropertySelected === 0) {
  			popup("<p>One or more properties must be selected in order to trade.</p>");
  
  			return false;
  		}
  
  		if (initiator.human && !confirm(initiator.name + ", are you sure you want to make this offer to " + recipient.name + "?")) {
  			return false;
  		}
  
  		var reversedTrade = new Trade(recipient, initiator, -money, reversedTradeProperty, -tradeObj.getCommunityChestJailCard(), -tradeObj.getChanceJailCard());
  
  		if (recipient.human) {
  
  			writeTrade(reversedTrade);
  
  			$("#proposetradebutton").hide();
  			$("#canceltradebutton").hide();
  			$("#accepttradebutton").show();
  			$("#rejecttradebutton").show();
  
  			addAlert(initiator.name + " initiated a trade with " + recipient.name + ".");
  			popup("<p>" + initiator.name + " has proposed a trade with you, " + recipient.name + ". You may accept, reject, or modify the offer.</p>");
  		} else {
  			var tradeResponse = recipient.AI.acceptTrade(tradeObj);
  
  			if (tradeResponse === true) {
  				popup("<p>" + recipient.name + " has accepted your offer.</p>");
  				this.acceptTrade(reversedTrade);
  			} else if (tradeResponse === false) {
  				popup("<p>" + recipient.name + " has declined your offer.</p>");
  				return;
  			} else if (tradeResponse instanceof Trade) {
  				popup("<p>" + recipient.name + " has proposed a counteroffer.</p>");
  				writeTrade(tradeResponse);
  
  				$("#proposetradebutton, #canceltradebutton").hide();
  				$("#accepttradebutton").show();
  				$("#rejecttradebutton").show();
  			}
  		}
  	};
  
  
  
  	// Bankrupcy functions:
  
  
  
  
  	this.eliminatePlayer = function() {
  		var p = player[turn];
  
  		for (var i = p.index; i < pcount; i++) {
  			player[i] = player[i + 1];
  			player[i].index = i;
  
  		}
  
  		for (var i = 0; i < 40; i++) {
  			if (square[i].owner >= p.index) {
  				square[i].owner--;
  			}
  		}
  
  		pcount--;
  		turn--;
  
  		if (pcount === 2) {
  			document.getElementById("stats").style.width = "454px";
  		} else if (pcount === 3) {
  			document.getElementById("stats").style.width = "686px";
  		}
  
  		if (pcount === 1) {
  			updateMoney();
  			$("#control").hide();
  			$("#board").hide();
  			$("#refresh").show();
  
  			// // Display land counts for survey purposes.
  			// var text;
  			// for (var i = 0; i < 40; i++) {
  				// if (i === 0)
  					// text = square[i].landcount;
  				// else
  					// text += " " + square[i].landcount;
  			// }
  			// document.getElementById("refresh").innerHTML += "<br><br><div><textarea type='text' style='width: 980px;' onclick='javascript:select();' />" + text + "</textarea></div>";
  
  			popup("<p>Congratulations, " + player[1].name + ", you have won the game.</p><div>");
  
  		} else {
  			play();
  		}
  	};
  
  	this.bankruptcyUnmortgage = function() {
  		var p = player[turn];
  
  		if (p.creditor === 0) {
  			this.eliminatePlayer();
  			return;
  		}
  
  		var HTML = "<p>" + player[p.creditor].name + ", you may unmortgage any of the following properties, interest free, by clicking on them. Click OK when finished.</p><table>";
  		var price;
  
  		for (var i = 0; i < 40; i++) {
  			sq = square[i];
  			if (sq.owner == p.index && sq.mortgage) {
  				price = Math.round(sq.price * 0.5);
  
  				HTML += "<tr><td class='propertycellcolor' style='background: " + sq.color + ";";
  
  				if (sq.groupNumber == 1 || sq.groupNumber == 2) {
  					HTML += " border: 1px solid grey;";
  				} else {
  					HTML += " border: 1px solid " + sq.color + ";";
  				}
  
  				// Player already paid interest, so they can unmortgage for the mortgage price.
  				HTML += "' onmouseover='showdeed(" + i + ");' onmouseout='hidedeed();'></td><td class='propertycellname'><a href='javascript:void(0);' title='Unmortgage " + sq.name + " for $" + price + ".' onclick='if (" + price + " <= player[" + p.creditor + "].money) {player[" + p.creditor + "].pay(" + price + ", 0); square[" + i + "].mortgage = false; addAlert(\"" + player[p.creditor].name + " unmortgaged " + sq.name + " for $" + price + ".\");} this.parentElement.parentElement.style.display = \"none\";'>Unmortgage " + sq.name + " ($" + price + ")</a></td></tr>";
  
  				sq.owner = p.creditor;
  
  			}
  		}
  
  		HTML += "</table>";
  
  		popup(HTML, game.eliminatePlayer);
  	};
  
  	this.resign = function() {
  		popup("<p>Are you sure you want to resign?</p>", game.bankruptcy, "Yes/No");
  	};
  
  	this.bankruptcy = function() {
  		var p = player[turn];
  		var pcredit = player[p.creditor];
  		var bankruptcyUnmortgageFee = 0;
  
  
  		if (p.money >= 0) {
  			return;
  		}
  
  		addAlert(p.name + " is bankrupt.");
  
  		if (p.creditor !== 0) {
  			pcredit.money += p.money;
  		}
  
  		for (var i = 0; i < 40; i++) {
  			sq = square[i];
  			if (sq.owner == p.index) {
  				// Mortgaged properties will be tranfered by bankruptcyUnmortgage();
  				if (!sq.mortgage) {
  					sq.owner = p.creditor;
  				} else {
  					bankruptcyUnmortgageFee += Math.round(sq.price * 0.1);
  				}
  
  				if (sq.house > 0) {
  					if (p.creditor !== 0) {
  						pcredit.money += sq.houseprice * 0.5 * sq.house;
  					}
  					sq.hotel = 0;
  					sq.house = 0;
  				}
  
  				if (p.creditor === 0) {
  					sq.mortgage = false;
  					game.addPropertyToAuctionQueue(i);
  					sq.owner = 0;
  				}
  			}
  		}
  
  		updateMoney();
  
  		if (p.chanceJailCard) {
  			p.chanceJailCard = false;
  			pcredit.chanceJailCard = true;
  		}
  
  		if (p.communityChestJailCard) {
  			p.communityChestJailCard = false;
  			pcredit.communityChestJailCard = true;
  		}
  
  		if (pcount === 2 || bankruptcyUnmortgageFee === 0 || p.creditor === 0) {
  			this.eliminatePlayer();
  		} else {
  			addAlert(pcredit.name + " paid $" + bankruptcyUnmortgageFee + " interest on the mortgaged properties received from " + p.name + ".");
  			popup("<p>" + pcredit.name + ", you must pay $" + bankruptcyUnmortgageFee + " interest on the mortgaged properties you received from " + p.name + ".</p>", function() {player[pcredit.index].pay(bankruptcyUnmortgageFee, 0); game.bankruptcyUnmortgage();});
  		}
  	};
  
  }
  
  var game;
  
  
  function Player(name, color) {
  	this.name = name;
  	this.color = color;
  	this.position = 0;
  	this.money = 1500;
  	this.creditor = -1;
  	this.jail = false;
  	this.jailroll = 0;
  	this.communityChestJailCard = false;
  	this.chanceJailCard = false;
  	this.bidding = true;
  	this.human = true;
  	// this.AI = null;
  
  	this.pay = function (amount, creditor) {
  		if (amount <= this.money) {
  			this.money -= amount;
  
  			updateMoney();
  
  			return true;
  		} else {
  			this.money -= amount;
  			this.creditor = creditor;
  
  			updateMoney();
  
  			return false;
  		}
  	};
  }
  
  // paramaters:
  // initiator: object Player
  // recipient: object Player
  // money: integer, positive for offered, negative for requested
  // property: array of integers, length: 40
  // communityChestJailCard: integer, 1 means offered, -1 means requested, 0 means neither
  // chanceJailCard: integer, 1 means offered, -1 means requested, 0 means neither
  function Trade(initiator, recipient, money, property, communityChestJailCard, chanceJailCard) {
  	// For each property and get out of jail free cards, 1 means offered, -1 means requested, 0 means neither.
  
  	this.getInitiator = function() {
  		return initiator;
  	};
  
  	this.getRecipient = function() {
  		return recipient;
  	};
  
  	this.getProperty = function(index) {
  		return property[index];
  	};
  
  	this.getMoney = function() {
  		return money;
  	};
  
  	this.getCommunityChestJailCard = function() {
  		return communityChestJailCard;
  	};
  
  	this.getChanceJailCard = function() {
  		return chanceJailCard;
  	};
  }
  
  var player = [];
  var pcount;
  var turn = 0, doublecount = 0;
  // Overwrite an array with numbers from one to the array's length in a random order.
  Array.prototype.randomize = function(length) {
  	length = (length || this.length);
  	var num;
  	var indexArray = [];
  
  	for (var i = 0; i < length; i++) {
  		indexArray[i] = i;
  	}
  
  	for (var i = 0; i < length; i++) {
  		// Generate random number between 0 and indexArray.length - 1.
  		num = Math.floor(Math.random() * indexArray.length);
  		this[i] = indexArray[num] + 1;
  
  		indexArray.splice(num, 1);
  	}
  };
  
  // function show(element) {
  	// // Element may be an HTML element or the id of one passed as a string.
  	// if (element.constructor == String) {
  		// element = document.getElementById(element);
  	// }
  
  	// if (element.tagName == "INPUT" || element.tagName == "SPAN" || element.tagName == "LABEL") {
  		// element.style.display = "inline";
  	// } else {
  		// element.style.display = "block";
  	// }
  // }
  
  // function hide(element) {
  	// // Element may be an HTML element or the id of one passed as a string.
  	// if (element.constructor == String) {
  		// document.getElementById(element).style.display = "none";
  	// } else {
  		// element.style.display = "none";
  	// }
  // }
  
  function addAlert(alertText) {
  	$alert = $("#alert");
  
  	$(document.createElement("div")).text(alertText).appendTo($alert);
  
  	// Animate scrolling down alert element.
  	$alert.stop().animate({"scrollTop": $alert.prop("scrollHeight")}, 1000);
  
  	if (!player[turn].human) {
  		player[turn].AI.alertList += "<div>" + alertText + "</div>";
  	}
  }
  
  function popup(HTML, action, option) {
  	document.getElementById("popuptext").innerHTML = HTML;
  	document.getElementById("popup").style.width = "300px";
  	document.getElementById("popup").style.top = "0px";
  	document.getElementById("popup").style.left = "0px";
  
  	if (!option && typeof action === "string") {
  		option = action;
  	}
  
  	option = option ? option.toLowerCase() : "";
  
  	if (typeof action !== "function") {
  		action = null;
  	}
  
  	// blank
  	if (option === "blank") {
  		// do nothing
  
  	// Yes/No
  	} else if (option === "yes/no") {
  		document.getElementById("popuptext").innerHTML += "<div><input type=\"button\" value=\"Yes\" id=\"popupyes\" /><input type=\"button\" value=\"No\" id=\"popupno\" /></div>";
  
  		$("#popupyes, #popupno").on("click", function() {
  			$("#popupwrap").hide();
  			$("#popupbackground").fadeOut(400);
  		});
  
  		$("#popupyes").on("click", action);
  
  	// Ok
  	} else if (option === "") {
  		$("#popuptext").append("<div><input type='button' value='OK' id='popupclose' /></div>");
  		$("#popupclose").focus();
  
  		$("#popupclose").on("click", function() {
  			$("#popupwrap").hide();
  			$("#popupbackground").fadeOut(400);
  		}).on("click", action);
  
  	// unknown
  	} else {
  		alert("unknown popup option '"+option+"'")
  	}
  
  	// Show using animation.
  	$("#popupbackground").fadeIn(400, function() {
  		$("#popupwrap").show();
  	});
  
  }
  
  
  function updatePosition() {
  	// Reset borders
  	document.getElementById("jail").style.border = "1px solid black";
  	document.getElementById("jailpositionholder").innerHTML = "";
  	for (var i = 0; i < 40; i++) {
  		document.getElementById("cell" + i).style.border = "1px solid black";
  		document.getElementById("cell" + i + "positionholder").innerHTML = "";
  
  	}
  
  	var sq, left, top;
  
  	for (var x = 0; x < 40; x++) {
  		sq = square[x];
  		left = 0;
  		top = 0;
  
  		for (var y = turn; y <= pcount; y++) {
  
  			if (player[y].position == x && !player[y].jail) {
  
  				document.getElementById("cell" + x + "positionholder").innerHTML += "<div class='cell-position' title='" + player[y].name + "' style='background-color: " + player[y].color + "; left: " + left + "px; top: " + top + "px;'></div>";
  				if (left == 36) {
  					left = 0;
  					top = 12;
  				} else
  					left += 12;
  			}
  		}
  
  		for (var y = 1; y < turn; y++) {
  
  			if (player[y].position == x && !player[y].jail) {
  				document.getElementById("cell" + x + "positionholder").innerHTML += "<div class='cell-position' title='" + player[y].name + "' style='background-color: " + player[y].color + "; left: " + left + "px; top: " + top + "px;'></div>";
  				if (left == 36) {
  					left = 0;
  					top = 12;
  				} else
  					left += 12;
  			}
  		}
  	}
  
  	left = 0;
  	top = 53;
  	for (var i = turn; i <= pcount; i++) {
  		if (player[i].jail) {
  			document.getElementById("jailpositionholder").innerHTML += "<div class='cell-position' title='" + player[i].name + "' style='background-color: " + player[i].color + "; left: " + left + "px; top: " + top + "px;'></div>";
  
  			if (left === 36) {
  				left = 0;
  				top = 41;
  			} else {
  				left += 12;
  			}
  		}
  	}
  
  	for (var i = 1; i < turn; i++) {
  		if (player[i].jail) {
  			document.getElementById("jailpositionholder").innerHTML += "<div class='cell-position' title='" + player[i].name + "' style='background-color: " + player[i].color + "; left: " + left + "px; top: " + top + "px;'></div>";
  			if (left === 36) {
  				left = 0;
  				top = 41;
  			} else
  				left += 12;
  		}
  	}
  
  	p = player[turn];
  
  	if (p.jail) {
  		document.getElementById("jail").style.border = "1px solid " + p.color;
  	} else {
  		document.getElementById("cell" + p.position).style.border = "1px solid " + p.color;
  	}
  
  	// for (var i=1; i <= pcount; i++) {
  	// document.getElementById("enlarge"+player[i].position+"token").innerHTML+="<img src='"+tokenArray[i].src+"' height='30' width='30' />";
  	// }
  }
  
  function updateMoney() {
  	var p = player[turn];
  
  	document.getElementById("pmoney").innerHTML = "$" + p.money;
  	$(".money-bar-row").hide();
  
  	for (var i = 1; i <= pcount; i++) {
  		p_i = player[i];
  
  		$("#moneybarrow" + i).show();
  		document.getElementById("p" + i + "moneybar").style.border = "2px solid " + p_i.color;
  		document.getElementById("p" + i + "money").innerHTML = p_i.money;
  		document.getElementById("p" + i + "moneyname").innerHTML = p_i.name;
  	}
  	// show("moneybarrow9"); // Don't remove this line or make the first for-loop stop when i <= 8, because this affects how the table is displayed.
  
  	if (document.getElementById("landed").innerHTML === "") {
  		$("#landed").hide();
  	}
  
  	document.getElementById("quickstats").style.borderColor = p.color;
  
  	if (p.money < 0) {
  		// document.getElementById("nextbutton").disabled = true;
  		$("#resignbutton").show();
  		$("#nextbutton").hide();
  	} else {
  		// document.getElementById("nextbutton").disabled = false;
  		$("#resignbutton").hide();
  		$("#nextbutton").show();
  	}
  }
  
  function updateDice() {
  	var die0 = game.getDie(1);
  	var die1 = game.getDie(2);
  
  	$("#die0").show();
  	$("#die1").show();
  
  	if (document.images) {
  		var element0 = document.getElementById("die0");
  		var element1 = document.getElementById("die1");
  
  		element0.classList.remove("die-no-img");
  		element1.classList.remove("die-no-img");
  
  		element0.title = "Die (" + die0 + " spots)";
  		element1.title = "Die (" + die1 + " spots)";
  
  		if (element0.firstChild) {
  			element0 = element0.firstChild;
  		} else {
  			element0 = element0.appendChild(document.createElement("img"));
  		}
  
  		element0.src = "images/Die_" + die0 + ".png";
  		element0.alt = die0;
  
  		if (element1.firstChild) {
  			element1 = element1.firstChild;
  		} else {
  			element1 = element1.appendChild(document.createElement("img"));
  		}
  
  		element1.src = "images/Die_" + die1 + ".png";
  		element1.alt = die0;
  	} else {
  		document.getElementById("die0").textContent = die0;
  		document.getElementById("die1").textContent = die1;
  
  		document.getElementById("die0").title = "Die";
  		document.getElementById("die1").title = "Die";
  	}
  }
  
  function updateOwned() {
  	var p = player[turn];
  	var checkedproperty = getCheckedProperty();
  	$("#option").show();
  	$("#owned").show();
  
  	var HTML = "",
  	firstproperty = -1;
  
  	var mortgagetext = "",
  	housetext = "";
  	var sq;
  
  	for (var i = 0; i < 40; i++) {
  		sq = square[i];
  		if (sq.groupNumber && sq.owner === 0) {
  			$("#cell" + i + "owner").hide();
  		} else if (sq.groupNumber && sq.owner > 0) {
  			var currentCellOwner = document.getElementById("cell" + i + "owner");
  
  			currentCellOwner.style.display = "block";
  			currentCellOwner.style.backgroundColor = player[sq.owner].color;
  			currentCellOwner.title = player[sq.owner].name;
  		}
  	}
  
  	for (var i = 0; i < 40; i++) {
  		sq = square[i];
  		if (sq.owner == turn) {
  
  			mortgagetext = "";
  			if (sq.mortgage) {
  				mortgagetext = "title='Mortgaged' style='color: grey;'";
  			}
  
  			housetext = "";
  			if (sq.house >= 1 && sq.house <= 4) {
  				for (var x = 1; x <= sq.house; x++) {
  					housetext += "<img src='images/house.png' alt='' title='House' class='house' />";
  				}
  			} else if (sq.hotel) {
  				housetext += "<img src='images/hotel.png' alt='' title='Hotel' class='hotel' />";
  			}
  
  			if (HTML === "") {
  				HTML += "<table>";
  				firstproperty = i;
  			}
  
  			HTML += "<tr class='property-cell-row'><td class='propertycellcheckbox'><input type='checkbox' id='propertycheckbox" + i + "' /></td><td class='propertycellcolor' style='background: " + sq.color + ";";
  
  			if (sq.groupNumber == 1 || sq.groupNumber == 2) {
  				HTML += " border: 1px solid grey; width: 18px;";
  			}
  
  			HTML += "' onmouseover='showdeed(" + i + ");' onmouseout='hidedeed();'></td><td class='propertycellname' " + mortgagetext + ">" + sq.name + housetext + "</td></tr>";
  		}
  	}
  
  	if (p.communityChestJailCard) {
  		if (HTML === "") {
  			firstproperty = 40;
  			HTML += "<table>";
  		}
  		HTML += "<tr class='property-cell-row'><td class='propertycellcheckbox'><input type='checkbox' id='propertycheckbox40' /></td><td class='propertycellcolor' style='background: white;'></td><td class='propertycellname'>Get Out of Jail Free Card</td></tr>";
  
  	}
  	if (p.chanceJailCard) {
  		if (HTML === "") {
  			firstproperty = 41;
  			HTML += "<table>";
  		}
  		HTML += "<tr class='property-cell-row'><td class='propertycellcheckbox'><input type='checkbox' id='propertycheckbox41' /></td><td class='propertycellcolor' style='background: white;'></td><td class='propertycellname'>Get Out of Jail Free Card</td></tr>";
  	}
  
  	if (HTML === "") {
  		HTML = p.name + ", you don't have any properties.";
  		$("#option").hide();
  	} else {
  		HTML += "</table>";
  	}
  
  	document.getElementById("owned").innerHTML = HTML;
  
  	// Select previously selected property.
  	if (checkedproperty > -1 && document.getElementById("propertycheckbox" + checkedproperty)) {
  		document.getElementById("propertycheckbox" + checkedproperty).checked = true;
  	} else if (firstproperty > -1) {
  		document.getElementById("propertycheckbox" + firstproperty).checked = true;
  	}
  	$(".property-cell-row").click(function() {
  		var row = this;
  
  		// Toggle check the current checkbox.
  		$(this).find(".propertycellcheckbox > input").prop("checked", function(index, val) {
  			return !val;
  		});
  
  		// Set all other checkboxes to false.
  		$(".propertycellcheckbox > input").prop("checked", function(index, val) {
  			if (!$.contains(row, this)) {
  				return false;
  			}
  		});
  
  		updateOption();
  	});
  	updateOption();
  }
  
  function updateOption() {
  	$("#option").show();
  
  	var allGroupUninproved = true;
  	var allGroupUnmortgaged = true;
  	var checkedproperty = getCheckedProperty();
  
  	if (checkedproperty < 0 || checkedproperty >= 40) {
  		$("#buyhousebutton").hide();
  		$("#sellhousebutton").hide();
  		$("#mortgagebutton").hide();
  
  
  		var housesum = 32;
  		var hotelsum = 12;
  
  		for (var i = 0; i < 40; i++) {
  			s = square[i];
  			if (s.hotel == 1)
  				hotelsum--;
  			else
  				housesum -= s.house;
  		}
  
  		$("#buildings").show();
  		document.getElementById("buildings").innerHTML = "<img src='images/house.png' alt='' title='House' class='house' />:&nbsp;" + housesum + "&nbsp;&nbsp;<img src='images/hotel.png' alt='' title='Hotel' class='hotel' />:&nbsp;" + hotelsum;
  
  		return;
  	}
  
  	$("#buildings").hide();
  	var sq = square[checkedproperty];
  
  	buyhousebutton = document.getElementById("buyhousebutton");
  	sellhousebutton = document.getElementById("sellhousebutton");
  
  	$("#mortgagebutton").show();
  	document.getElementById("mortgagebutton").disabled = false;
  
  	if (sq.mortgage) {
  		document.getElementById("mortgagebutton").value = "Unmortgage ($" + Math.round(sq.price * 0.6) + ")";
  		document.getElementById("mortgagebutton").title = "Unmortgage " + sq.name + " for $" + Math.round(sq.price * 0.6) + ".";
  		$("#buyhousebutton").hide();
  		$("#sellhousebutton").hide();
  
  		allGroupUnmortgaged = false;
  	} else {
  		document.getElementById("mortgagebutton").value = "Mortgage ($" + (sq.price * 0.5) + ")";
  		document.getElementById("mortgagebutton").title = "Mortgage " + sq.name + " for $" + (sq.price * 0.5) + ".";
  
  		if (sq.groupNumber >= 3) {
  			$("#buyhousebutton").show();
  			$("#sellhousebutton").show();
  			buyhousebutton.disabled = false;
  			sellhousebutton.disabled = false;
  
  			buyhousebutton.value = "Buy house ($" + sq.houseprice + ")";
  			sellhousebutton.value = "Sell house ($" + (sq.houseprice * 0.5) + ")";
  			buyhousebutton.title = "Buy a house for $" + sq.houseprice;
  			sellhousebutton.title = "Sell a house for $" + (sq.houseprice * 0.5);
  
  			if (sq.house == 4) {
  				buyhousebutton.value = "Buy hotel ($" + sq.houseprice + ")";
  				buyhousebutton.title = "Buy a hotel for $" + sq.houseprice;
  			}
  			if (sq.hotel == 1) {
  				$("#buyhousebutton").hide();
  				sellhousebutton.value = "Sell hotel ($" + (sq.houseprice * 0.5) + ")";
  				sellhousebutton.title = "Sell a hotel for $" + (sq.houseprice * 0.5);
  			}
  
  			var maxhouse = 0;
  			var minhouse = 5;
  
  			for (var j = 0; j < max; j++) {
  
  				if (square[currentSquare.group[j]].house > 0) {
  					allGroupUninproved = false;
  					break;
  				}
  			}
  
  			var max = sq.group.length;
  			for (var i = 0; i < max; i++) {
  				s = square[sq.group[i]];
  
  				if (s.owner !== sq.owner) {
  					buyhousebutton.disabled = true;
  					sellhousebutton.disabled = true;
  					buyhousebutton.title = "Before you can buy a house, you must own all the properties of this color-group.";
  				} else {
  
  					if (s.house > maxhouse) {
  						maxhouse = s.house;
  					}
  
  					if (s.house < minhouse) {
  						minhouse = s.house;
  					}
  
  					if (s.house > 0) {
  						allGroupUninproved = false;
  					}
  
  					if (s.mortgage) {
  						allGroupUnmortgaged = false;
  					}
  				}
  			}
  
  			if (!allGroupUnmortgaged) {
  				buyhousebutton.disabled = true;
  				buyhousebutton.title = "Before you can buy a house, you must unmortgage all the properties of this color-group.";
  			}
  
  			// Force even building
  			if (sq.house > minhouse) {
  				buyhousebutton.disabled = true;
  
  				if (sq.house == 1) {
  					buyhousebutton.title = "Before you can buy another house, the other properties of this color-group must all have one house.";
  				} else if (sq.house == 4) {
  					buyhousebutton.title = "Before you can buy a hotel, the other properties of this color-group must all have 4 houses.";
  				} else {
  					buyhousebutton.title = "Before you can buy a house, the other properties of this color-group must all have " + sq.house + " houses.";
  				}
  			}
  			if (sq.house < maxhouse) {
  				sellhousebutton.disabled = true;
  
  				if (sq.house == 1) {
  					sellhousebutton.title = "Before you can sell house, the other properties of this color-group must all have one house.";
  				} else {
  					sellhousebutton.title = "Before you can sell a house, the other properties of this color-group must all have " + sq.house + " houses.";
  				}
  			}
  
  			if (sq.house === 0 && sq.hotel === 0) {
  				$("#sellhousebutton").hide();
  
  			} else {
  				$("#mortgagebutton").hide();
  
  			}
  
  			// Before a property can be mortgaged or sold, all the properties of its color-group must unimproved.
  			if (!allGroupUninproved) {
  				document.getElementById("mortgagebutton").title = "Before a property can be mortgaged, all the properties of its color-group must unimproved.";
  				document.getElementById("mortgagebutton").disabled = true;
  			}
  
  		} else {
  			$("#buyhousebutton").hide();
  			$("#sellhousebutton").hide();
  		}
  	}
  }
  
  function chanceCommunityChest() {
  	var p = player[turn];
  
  	// Community Chest
  	if (p.position === 2 || p.position === 17 || p.position === 33) {
  		var communityChestIndex = communityChestCards.deck[communityChestCards.index];
  
  		// Remove the get out of jail free card from the deck.
  		if (communityChestIndex === 0) {
  			communityChestCards.deck.splice(communityChestCards.index, 1);
  		}
  
  		popup("<img src='images/community_chest_icon.png' style='height: 50px; width: 53px; float: left; margin: 8px 8px 8px 0px;' /><div style='font-weight: bold; font-size: 16px; '>Community Chest:</div><div style='text-align: justify;'>" + communityChestCards[communityChestIndex].text + "</div>", function() {
  			communityChestAction(communityChestIndex);
  		});
  
  		communityChestCards.index++;
  
  		if (communityChestCards.index >= communityChestCards.deck.length) {
  			communityChestCards.index = 0;
  		}
  
  	// Chance
  	} else if (p.position === 7 || p.position === 22 || p.position === 36) {
  		var chanceIndex = chanceCards.deck[chanceCards.index];
  
  		// Remove the get out of jail free card from the deck.
  		if (chanceIndex === 0) {
  			chanceCards.deck.splice(chanceCards.index, 1);
  		}
  
  		popup("<img src='images/chance_icon.png' style='height: 50px; width: 26px; float: left; margin: 8px 8px 8px 0px;' /><div style='font-weight: bold; font-size: 16px; '>Chance:</div><div style='text-align: justify;'>" + chanceCards[chanceIndex].text + "</div>", function() {
  			chanceAction(chanceIndex);
  		});
  
  		chanceCards.index++;
  
  		if (chanceCards.index >= chanceCards.deck.length) {
  			chanceCards.index = 0;
  		}
  	} else {
  		if (!p.human) {
  			p.AI.alertList = "";
  
  			if (!p.AI.onLand()) {
  				game.next();
  			}
  		}
  	}
  }
  
  function chanceAction(chanceIndex) {
  	var p = player[turn]; // This is needed for reference in action() method.
  
  	// $('#popupbackground').hide();
  	// $('#popupwrap').hide();
  	chanceCards[chanceIndex].action(p);
  
  	updateMoney();
  
  	if (chanceIndex !== 15 && !p.human) {
  		p.AI.alertList = "";
  		game.next();
  	}
  }
  
  function communityChestAction(communityChestIndex) {
  	var p = player[turn]; // This is needed for reference in action() method.
  
  	// $('#popupbackground').hide();
  	// $('#popupwrap').hide();
  	communityChestCards[communityChestIndex].action(p);
  
  	updateMoney();
  
  	if (communityChestIndex !== 15 && !p.human) {
  		p.AI.alertList = "";
  		game.next();
  	}
  }
  
  function addamount(amount, cause) {
  	var p = player[turn];
  
  	p.money += amount;
  
  	addAlert(p.name + " received $" + amount + " from " + cause + ".");
  }
  
  function subtractamount(amount, cause) {
  	var p = player[turn];
  
  	p.pay(amount, 0);
  
  	addAlert(p.name + " lost $" + amount + " from " + cause + ".");
  }
  
  function gotojail() {
  	var p = player[turn];
  	addAlert(p.name + " was sent directly to jail.");
  	document.getElementById("landed").innerHTML = "You are in jail.";
  
  	p.jail = true;
  	doublecount = 0;
  
  	document.getElementById("nextbutton").value = "End turn";
  	document.getElementById("nextbutton").title = "End turn and advance to the next player.";
  
  	if (p.human) {
  		document.getElementById("nextbutton").focus();
  	}
  
  	updatePosition();
  	updateOwned();
  
  	if (!p.human) {
  		popup(p.AI.alertList, game.next);
  		p.AI.alertList = "";
  	}
  }
  
  function gobackthreespaces() {
  	var p = player[turn];
  
  	p.position -= 3;
  
  	land();
  }
  
  function payeachplayer(amount, cause) {
  	var p = player[turn];
  	var total = 0;
  
  	for (var i = 1; i <= pcount; i++) {
  		if (i != turn) {
  			player[i].money += amount;
  			total += amount;
  			creditor = p.money >= 0 ? i : creditor;
  
  			p.pay(amount, creditor);
  		}
  	}
  
  	addAlert(p.name + " lost $" + total + " from " + cause + ".");
  }
  
  function collectfromeachplayer(amount, cause) {
  	var p = player[turn];
  	var total = 0;
  
  	for (var i = 1; i <= pcount; i++) {
  		if (i != turn) {
  			money = player[i].money;
  			if (money < amount) {
  				p.money += money;
  				total += money;
  				player[i].money = 0;
  			} else {
  				player[i].pay(amount, turn);
  				p.money += amount;
  				total += amount;
  			}
  		}
  	}
  
  	addAlert(p.name + " received $" + total + " from " + cause + ".");
  }
  
  function advance(destination, pass) {
  	var p = player[turn];
  
  	if (typeof pass === "number") {
  		if (p.position < pass) {
  			p.position = pass;
  		} else {
  			p.position = pass;
  			p.money += 200;
  			addAlert(p.name + " collected a $200 salary for passing GO.");
  		}
  	}
  	if (p.position < destination) {
  		p.position = destination;
  	} else {
  		p.position = destination;
  		p.money += 200;
  		addAlert(p.name + " collected a $200 salary for passing GO.");
  	}
  
  	land();
  }
  
  function advanceToNearestUtility() {
  	var p = player[turn];
  
  	if (p.position < 12) {
  		p.position = 12;
  	} else if (p.position >= 12 && p.position < 28) {
  		p.position = 28;
  	} else if (p.position >= 28) {
  		p.position = 12;
  		p.money += 200;
  		addAlert(p.name + " collected a $200 salary for passing GO.");
  	}
  
  	land(true);
  }
  
  function advanceToNearestRailroad() {
  	var p = player[turn];
  
  	updatePosition();
  
  	if (p.position < 15) {
  		p.position = 15;
  	} else if (p.position >= 15 && p.position < 25) {
  		p.position = 25;
  	} else if (p.position >= 35) {
  		p.position = 5;
  		p.money += 200;
  		addAlert(p.name + " collected a $200 salary for passing GO.");
  	}
  
  	land(true);
  }
  
  function streetrepairs(houseprice, hotelprice) {
  	var cost = 0;
  	for (var i = 0; i < 40; i++) {
  		var s = square[i];
  		if (s.owner == turn) {
  			if (s.hotel == 1)
  				cost += hotelprice;
  			else
  				cost += s.house * houseprice;
  		}
  	}
  
  	var p = player[turn];
  
  	if (cost > 0) {
  		p.pay(cost, 0);
  
  		// If function was called by Community Chest.
  		if (houseprice === 40) {
  			addAlert(p.name + " lost $" + cost + " to Community Chest.");
  		} else {
  			addAlert(p.name + " lost $" + cost + " to Chance.");
  		}
  	}
  
  }
  
  function payfifty() {
  	var p = player[turn];
  
  	document.getElementById("jail").style.border = '1px solid black';
  	document.getElementById("cell11").style.border = '2px solid ' + p.color;
  
  	$("#landed").hide();
  	doublecount = 0;
  
  	p.jail = false;
  	p.jailroll = 0;
  	p.position = 10;
  	p.pay(50, 0);
  
  	addAlert(p.name + " paid the $50 fine to get out of jail.");
  	updateMoney();
  	updatePosition();
  }
  
  function useJailCard() {
  	var p = player[turn];
  
  	document.getElementById("jail").style.border = '1px solid black';
  	document.getElementById("cell11").style.border = '2px solid ' + p.color;
  
  	$("#landed").hide();
  	p.jail = false;
  	p.jailroll = 0;
  
  	p.position = 10;
  
  	doublecount = 0;
  
  	if (p.communityChestJailCard) {
  		p.communityChestJailCard = false;
  
  		// Insert the get out of jail free card back into the community chest deck.
  		communityChestCards.deck.splice(communityChestCards.index, 0, 0);
  
  		communityChestCards.index++;
  
  		if (communityChestCards.index >= communityChestCards.deck.length) {
  			communityChestCards.index = 0;
  		}
  	} else if (p.chanceJailCard) {
  		p.chanceJailCard = false;
  
  		// Insert the get out of jail free card back into the chance deck.
  		chanceCards.deck.splice(chanceCards.index, 0, 0);
  
  		chanceCards.index++;
  
  		if (chanceCards.index >= chanceCards.deck.length) {
  			chanceCards.index = 0;
  		}
  	}
  
  	addAlert(p.name + " used a \"Get Out of Jail Free\" card.");
  	updateOwned();
  	updatePosition();
  }
  
  function buyHouse(index) {
  	var sq = square[index];
  	var p = player[sq.owner];
  	var houseSum = 0;
  	var hotelSum = 0;
  
  	if (p.money - sq.houseprice < 0) {
  		if (sq.house == 4) {
  			return false;
  		} else {
  			return false;
  		}
  
  	} else {
  		for (var i = 0; i < 40; i++) {
  			if (square[i].hotel === 1) {
  				hotelSum++;
  			} else {
  				houseSum += square[i].house;
  			}
  		}
  
  		if (sq.house < 4) {
  			if (houseSum >= 32) {
  				return false;
  
  			} else {
  				sq.house++;
  				addAlert(p.name + " placed a house on " + sq.name + ".");
  			}
  
  		} else {
  			if (hotelSum >= 12) {
  				return;
  
  			} else {
  				sq.house = 5;
  				sq.hotel = 1;
  				addAlert(p.name + " placed a hotel on " + sq.name + ".");
  			}
  		}
  
  		p.pay(sq.houseprice, 0);
  
  		updateOwned();
  		updateMoney();
  	}
  }
  
  function sellHouse(index) {
  	sq = square[index];
  	p = player[sq.owner];
  
  	if (sq.hotel === 1) {
  		sq.hotel = 0;
  		sq.house = 4;
  		addAlert(p.name + " sold the hotel on " + sq.name + ".");
  	} else {
  		sq.house--;
  		addAlert(p.name + " sold a house on " + sq.name + ".");
  	}
  
  	p.money += sq.houseprice * 0.5;
  	updateOwned();
  	updateMoney();
  }
  
  function showStats() {
  	var HTML, sq, p;
  	var mortgagetext,
  	housetext;
  	var write;
  	HTML = "<table align='center'><tr>";
  
  	for (var x = 1; x <= pcount; x++) {
  		write = false;
  		p = player[x];
  		if (x == 5) {
  			HTML += "</tr><tr>";
  		}
  		HTML += "<td class='statscell' id='statscell" + x + "' style='border: 2px solid " + p.color + "' ><div class='statsplayername'>" + p.name + "</div>";
  
  		for (var i = 0; i < 40; i++) {
  			sq = square[i];
  
  			if (sq.owner == x) {
  				mortgagetext = "",
  				housetext = "";
  
  				if (sq.mortgage) {
  					mortgagetext = "title='Mortgaged' style='color: grey;'";
  				}
  
  				if (!write) {
  					write = true;
  					HTML += "<table>";
  				}
  
  				if (sq.house == 5) {
  					housetext += "<span style='float: right; font-weight: bold;'>1&nbsp;x&nbsp;<img src='images/hotel.png' alt='' title='Hotel' class='hotel' style='float: none;' /></span>";
  				} else if (sq.house > 0 && sq.house < 5) {
  					housetext += "<span style='float: right; font-weight: bold;'>" + sq.house + "&nbsp;x&nbsp;<img src='images/house.png' alt='' title='House' class='house' style='float: none;' /></span>";
  				}
  
  				HTML += "<tr><td class='statscellcolor' style='background: " + sq.color + ";";
  
  				if (sq.groupNumber == 1 || sq.groupNumber == 2) {
  					HTML += " border: 1px solid grey;";
  				}
  
  				HTML += "' onmouseover='showdeed(" + i + ");' onmouseout='hidedeed();'></td><td class='statscellname' " + mortgagetext + ">" + sq.name + housetext + "</td></tr>";
  			}
  		}
  
  		if (p.communityChestJailCard) {
  			if (!write) {
  				write = true;
  				HTML += "<table>";
  			}
  			HTML += "<tr><td class='statscellcolor'></td><td class='statscellname'>Get Out of Jail Free Card</td></tr>";
  
  		}
  		if (p.chanceJailCard) {
  			if (!write) {
  				write = true;
  				HTML += "<table>";
  			}
  			HTML += "<tr><td class='statscellcolor'></td><td class='statscellname'>Get Out of Jail Free Card</td></tr>";
  
  		}
  
  		if (!write) {
  			HTML += p.name + " dosen't have any properties.";
  		} else {
  			HTML += "</table>";
  		}
  
  		HTML += "</td>";
  	}
  	HTML += "</tr></table><div id='titledeed'></div>";
  
  	document.getElementById("statstext").innerHTML = HTML;
  	// Show using animation.
  	$("#statsbackground").fadeIn(400, function() {
  		$("#statswrap").show();
  	});
  }
  
  function showdeed(property) {
  	var sq = square[property];
  	$("#deed").show();
  
  	$("#deed-normal").hide();
  	$("#deed-mortgaged").hide();
  	$("#deed-special").hide();
  
  	if (sq.mortgage) {
  		$("#deed-mortgaged").show();
  		document.getElementById("deed-mortgaged-name").textContent = sq.name;
  		document.getElementById("deed-mortgaged-mortgage").textContent = (sq.price / 2);
  
  	} else {
  
  		if (sq.groupNumber >= 3) {
  			$("#deed-normal").show();
  			document.getElementById("deed-header").style.backgroundColor = sq.color;
  			document.getElementById("deed-name").textContent = sq.name;
  			document.getElementById("deed-question").textContent = sq.question;
  			document.getElementById("deed-baserent").textContent = sq.baserent;
  			document.getElementById("deed-rent1").textContent = sq.rent1;
  			document.getElementById("deed-rent2").textContent = sq.rent2;
  			document.getElementById("deed-rent3").textContent = sq.rent3;
  			document.getElementById("deed-rent4").textContent = sq.rent4;
  			document.getElementById("deed-rent5").textContent = sq.rent5;
  			document.getElementById("deed-mortgage").textContent = (sq.price / 2);
  			document.getElementById("deed-houseprice").textContent = sq.houseprice;
  			document.getElementById("deed-hotelprice").textContent = sq.houseprice;
  
  		} else if (sq.groupNumber == 2) {
  			$("#deed-special").show();
  			document.getElementById("deed-special-name").textContent = sq.name;
  			document.getElementById("deed-special-text").innerHTML = utiltext();
  			document.getElementById("deed-special-mortgage").textContent = (sq.price / 2);
  
  		} else if (sq.groupNumber == 1) {
  			$("#deed-special").show();
  			document.getElementById("deed-special-name").textContent = sq.name;
  			document.getElementById("deed-special-text").innerHTML = transtext();
  			document.getElementById("deed-special-mortgage").textContent = (sq.price / 2);
  		}
  	}
  }
  
  function hidedeed() {
  	$("#deed").hide();
  }
  
  function buy() {
  	var p = player[turn];
  	var property = square[p.position];
  	var cost = property.price;
  
  	if (p.money >= cost) {
  		p.pay(cost, 0);
  
  		property.owner = turn;
  		updateMoney();
  		addAlert(p.name + " bought " + property.name + " for " + property.pricetext + ".");
  
  		updateOwned();
  
  		$("#landed").hide();
  
  	} else {
  		popup("<p>" + p.name + ", you need $" + (property.price - p.money) + " more to buy " + property.name + ".</p>");
  	}
  }
  
  function mortgage(index) {
  	var sq = square[index];
  	var p = player[sq.owner];
  
  	if (sq.house > 0 || sq.hotel > 0 || sq.mortgage) {
  		return false;
  	}
  
  	var mortgagePrice = Math.round(sq.price * 0.5);
  	var unmortgagePrice = Math.round(sq.price * 0.6);
  
  	sq.mortgage = true;
  	p.money += mortgagePrice;
  
  	document.getElementById("mortgagebutton").value = "Unmortgage for $" + unmortgagePrice;
  	document.getElementById("mortgagebutton").title = "Unmortgage " + sq.name + " for $" + unmortgagePrice + ".";
  
  	addAlert(p.name + " mortgaged " + sq.name + " for $" + mortgagePrice + ".");
  	updateOwned();
  	updateMoney();
  
  	return true;
  }
  
  function unmortgage(index) {
  	var sq = square[index];
  	var p = player[sq.owner];
  	var unmortgagePrice = Math.round(sq.price * 0.6);
  	var mortgagePrice = Math.round(sq.price * 0.5);
  
  	if (unmortgagePrice > p.money || !sq.mortgage) {
  		return false;
  	}
  
  	p.pay(unmortgagePrice, 0);
  	sq.mortgage = false;
  	document.getElementById("mortgagebutton").value = "Mortgage for $" + mortgagePrice;
  	document.getElementById("mortgagebutton").title = "Mortgage " + sq.name + " for $" + mortgagePrice + ".";
  
  	addAlert(p.name + " unmortgaged " + sq.name + " for $" + unmortgagePrice + ".");
  	updateOwned();
  	return true;
  }
  
  
  function land(increasedRent) {
  	increasedRent = !!increasedRent; // Cast increasedRent to a boolean value. It is used for the ADVANCE TO THE NEAREST RAILROAD/UTILITY Chance cards.
  
  	var p = player[turn];
  	var s = square[p.position];
  
  	var die1 = game.getDie(1);
  	var die2 = game.getDie(2);
  
  	$("#landed").show();
  	document.getElementById("landed").innerHTML = "You landed on " + s.name + ".";
  	s.landcount++;
  	addAlert(p.name + " landed on " + s.name + ".");
  	
  	document.getElementById("question-text").innerHTML = s.question;
  	
  	// Allow player to buy the property on which he landed.
  	if (s.price !== 0 && s.owner === 0) {
  
  		if (!p.human) {
  
  			if (p.AI.buyProperty(p.position)) {
  				buy();
  			}
  		} else {
  			document.getElementById("landed").innerHTML = "<div>You landed on <a href='javascript:void(0);' onmouseover='showdeed(" + p.position + ");' onmouseout='hidedeed();' class='statscellcolor'>" + s.name + "</a>.<input type='button' onclick='buy();' value='Buy ($" + s.price + ")' title='Buy " + s.name + " for " + s.pricetext + ".'/></div>";
  		}
  
  
  		game.addPropertyToAuctionQueue(p.position);
  	}
  
  	// Collect rent
  	if (s.owner !== 0 && s.owner != turn && !s.mortgage) {
  		var groupowned = true;
  		var rent;
  
  		// Railroads
  		if (p.position == 5 || p.position == 15 || p.position == 25 || p.position == 35) {
  			if (increasedRent) {
  				rent = 25;
  			} else {
  				rent = 12.5;
  			}
  
  			if (s.owner == square[5].owner) {
  				rent *= 2;
  			}
  			if (s.owner == square[15].owner) {
  				rent *= 2;
  			}
  			if (s.owner == square[25].owner) {
  				rent *= 2;
  			}
  			if (s.owner == square[35].owner) {
  				rent *= 2;
  			}
  
  		} else if (p.position === 12) {
  			if (increasedRent || square[28].owner == s.owner) {
  				rent = (die1 + die2) * 10;
  			} else {
  				rent = (die1 + die2) * 4;
  			}
  
  		} else if (p.position === 28) {
  			if (increasedRent || square[12].owner == s.owner) {
  				rent = (die1 + die2) * 10;
  			} else {
  				rent = (die1 + die2) * 4;
  			}
  
  		} else {
  
  			for (var i = 0; i < 40; i++) {
  				sq = square[i];
  				if (sq.groupNumber == s.groupNumber && sq.owner != s.owner) {
  					groupowned = false;
  				}
  			}
  
  			if (!groupowned) {
  				rent = s.baserent;
  			} else {
  				if (s.house === 0) {
  					rent = s.baserent * 2;
  				} else {
  					rent = s["rent" + s.house];
  				}
  			}
  		}
  
  		addAlert(p.name + " paid $" + rent + " rent to " + player[s.owner].name + ".");
  		p.pay(rent, s.owner);
  		player[s.owner].money += rent;
  
  		document.getElementById("landed").innerHTML = "You landed on " + s.name + ". " + player[s.owner].name + " collected $" + rent + " rent.";
  	} else if (s.owner > 0 && s.owner != turn && s.mortgage) {
  		document.getElementById("landed").innerHTML = "You landed on " + s.name + ". Property is mortgaged; no rent was collected.";
  	}
  
  	// City Tax
  	if (p.position === 4) {
  		citytax();
  	}
  
  	// Go to Detention. Go directly to Jail. Do not pass GO. Do not collect $200.
  	if (p.position === 30) {
  		updateMoney();
  		updatePosition();
  
  		if (p.human) {
  			popup("<div>Go to detention. Go directly to Jail. Do not pass GO. Do not collect $200.</div>", goToJail);
  		} else {
  			gotojail();
  		}
  
  		return;
  	}
  
  	// Luxury Tax
  	if (p.position === 38) {
  		luxurytax();
  	}
  
  	updateMoney();
  	updatePosition();
  	updateOwned();
  
  	if (!p.human) {
  		popup(p.AI.alertList, chanceCommunityChest);
  		p.AI.alertList = "";
  	} else {
  		chanceCommunityChest();
  	}
  }
  
  function roll() {
  	var p = player[turn];
  
  	$("#option").hide();
  	$("#buy").show();
  	$("#manage").hide();
  
  	if (p.human) {
  		document.getElementById("nextbutton").focus();
  	}
  	document.getElementById("nextbutton").value = "End turn";
  	document.getElementById("nextbutton").title = "End turn and advance to the next player.";
  
  	game.rollDice();
  	var die1 = game.getDie(1);
  	var die2 = game.getDie(2);
  
  	doublecount++;
  
  	if (die1 == die2) {
  		addAlert(p.name + " rolled " + (die1 + die2) + " - doubles.");
  	} else {
  		addAlert(p.name + " rolled " + (die1 + die2) + ".");
  	}
  
  	if (die1 == die2 && !p.jail) {
  		updateDice(die1, die2);
  
  		if (doublecount < 3) {
  			document.getElementById("nextbutton").value = "Roll again";
  			document.getElementById("nextbutton").title = "You threw doubles. Roll again.";
  
  		// If player rolls doubles three times in a row, send him to jail
  		} else if (doublecount === 3) {
  			p.jail = true;
  			doublecount = 0;
  			addAlert(p.name + " rolled doubles three times in a row.");
  			updateMoney();
  
  
  			if (p.human) {
  				popup("You rolled doubles three times in a row. Go to jail.", goToJail);
  			} else {
  				gotojail();
  			}
  
  			return;
  		}
  	} else {
  		document.getElementById("nextbutton").value = "End turn";
  		document.getElementById("nextbutton").title = "End turn and advance to the next player.";
  		doublecount = 0;
  	}
  
  	updatePosition();
  	updateMoney();
  	updateOwned();
  
  	if (p.jail === true) {
  		p.jailroll++;
  
  		updateDice(die1, die2);
  		if (die1 == die2) {
  			document.getElementById("jail").style.border = "1px solid black";
  			document.getElementById("cell11").style.border = "2px solid " + p.color;
  			$("#landed").hide();
  
  			p.jail = false;
  			p.jailroll = 0;
  			p.position = 10 + die1 + die2;
  			doublecount = 0;
  
  			addAlert(p.name + " rolled doubles to get out of jail.");
  
  			land();
  		} else {
  			if (p.jailroll === 3) {
  
  				if (p.human) {
  					popup("<p>You must pay the $50 fine.</p>", function() {
  						payFifty();
  						payfifty();
  						player[turn].position=10 + die1 + die2;
  						land();
  					});
  				} else {
  					payfifty();
  					p.position = 10 + die1 + die2;
  					land();
  				}
  			} else {
  				$("#landed").show();
  				document.getElementById("landed").innerHTML = "You are in jail.";
  
  				if (!p.human) {
  					popup(p.AI.alertList, game.next);
  					p.AI.alertList = "";
  				}
  			}
  		}
  
  
  	} else {
  		updateDice(die1, die2);
  
  		// Move player
  		p.position += die1 + die2;
  
  		// Collect $200 salary as you pass GO
  		if (p.position >= 40) {
  			p.position -= 40;
  			p.money += 200;
  			addAlert(p.name + " collected a $200 salary for passing GO.");
  		}
  
  		land();
  	}
  }
  
  function play() {
  	if (game.auction()) {
  		return;
  	}
  
  	turn++;
  	if (turn > pcount) {
  		turn -= pcount;
  	}
  
  	var p = player[turn];
  	game.resetDice();
  
  	document.getElementById("pname").innerHTML = p.name;
  
  	addAlert("It is " + p.name + "'s turn.");
  
  	// Check for bankruptcy.
  	p.pay(0, p.creditor);
  
  	$("#landed, #option, #manage").hide();
  	$("#board, #control, #moneybar, #viewstats, #buy").show();
  
  	doublecount = 0;
  	if (p.human) {
  		document.getElementById("nextbutton").focus();
  	}
  	document.getElementById("nextbutton").value = "Roll Dice";
  	document.getElementById("nextbutton").title = "Roll the dice and move your token accordingly.";
  
  	$("#die0").hide();
  	$("#die1").hide();
  
  	if (p.jail) {
  		$("#landed").show();
  		document.getElementById("landed").innerHTML = "You are in jail.<input type='button' title='Pay $50 fine to get out of jail immediately.' value='Pay $50 fine' onclick='payfifty();' />";
  
  		if (p.communityChestJailCard || p.chanceJailCard) {
  			document.getElementById("landed").innerHTML += "<input type='button' id='gojfbutton' title='Use &quot;Get Out of Jail Free&quot; card.' onclick='useJailCard();' value='Use Card' />";
  		}
  
  		document.getElementById("nextbutton").title = "Roll the dice. If you throw doubles, you will get out of jail.";
  
  		if (p.jailroll === 0)
  			addAlert("This is " + p.name + "'s first turn in jail.");
  		else if (p.jailroll === 1)
  			addAlert("This is " + p.name + "'s second turn in jail.");
  		else if (p.jailroll === 2) {
  			document.getElementById("landed").innerHTML += "<div>NOTE: If you do not throw doubles after this roll, you <i>must</i> pay the $50 fine.</div>";
  			addAlert("This is " + p.name + "'s third turn in jail.");
  		}
  
  		if (!p.human && p.AI.postBail()) {
  			if (p.communityChestJailCard || p.chanceJailCard) {
  				useJailCard();
  			} else {
  				payfifty();
  			}
  		}
  	}
  
  	updateMoney();
  	updatePosition();
  	updateOwned();
  
  	$(".money-bar-arrow").hide();
  	$("#p" + turn + "arrow").show();
  
  	if (!p.human) {
  		if (!p.AI.beforeTurn()) {
  			game.next();
  		}
  	}
  }
  
  var game_ns = {
  	available_colors: ['blue', 'red', 'green', 'yellow', 'aqua', 'black', 'fuchsia', 'gray', 'lime', 'maroon', 'navy', 'olive', 'orange', 'purple', 'silver', 'teal',]
  }
  
  game_ns.draw_setup = function() {
  	var context = game_ns._draw_setup;
  
  	context.draw_player_wrappers(8);
  	context.bind_player_inteligence_change();
  	context.bind_and_invoke_player_color_change();
  	context.bind_and_invoke_players_count_change();
  }
  
  game_ns._draw_setup = {}
  
  game_ns._draw_setup.draw_player_wrappers = function(max) {
  	var i, color;
  
    var content = "";
  
  	var content_intel = "";
  	content_intel += "<select class='player-intel' title='Choose whether this player is controled by a human or by the computer.'>";
  	content_intel += "	<option value='0' selected='selected'>Human</option>";
  	content_intel += "	<option value='1'>AI (Test)</option>";
  	content_intel += "</select>";
  
    var content_colors = "<select class='player-color' title='Player color'>";
    for (i = 0; i <= game_ns.available_colors.length - 1; i++) {
    	color = game_ns.available_colors[i];
    	content_colors += "<option style='color: "+color+";'>"+color+"</option>";
    };
    content_colors += "</select>";
  
    for (i = 1; i <= max; i++) {
  	  content += "<div id='player"+i+"wrap' data-id='"+i+"' class='player-wrap'>";
  	  content += "Player "+i+": ";
  	  content += "<input type='text' class='player-name' title='Player name' maxlength='16' value='Player "+i+"' /> ";
  	  content += content_colors;
  	  content += " ";
  	  content += content_intel;
  	  content += "</div>";
    };
  
  	$("#player-wrappers").append(content);
  }
  
  game_ns._draw_setup.bind_player_inteligence_change = function() {
  	$("#player-wrappers .player-intel").change(function() {
  		var val = $(this).val();
  		var boo = val !== '0';
  		var wrap = $(this).closest('.player-wrap');
  
  		wrap.find('.player-name').attr('disabled', boo);
  		wrap.nextAll().find('.player-name').attr('disabled', boo);
  		wrap.nextAll().find('.player-intel').val(val);
  	});
  }
  
  game_ns._draw_setup.bind_and_invoke_player_color_change = function() {
  	$("#player-wrappers .player-color").on("change", game_ns._draw_setup.select_on_player_color_change);
  	$("#player-wrappers .player-color").change();
  }
  
  game_ns._draw_setup.bind_and_invoke_players_count_change = function() {
  	$("#playernumber").on("change", game_ns._draw_setup.select_on_player_number_change);
  	$("#playernumber").change();
  }
  
  
  game_ns._draw_setup.select_on_player_number_change = function() {
  	pcount = parseInt(document.getElementById("playernumber").value, 10);
  
  	$(".player-wrap").hide();
  
  	for (var i = 1; i <= pcount; i++) {
  		$("#player" + i + "wrap").show();
  	}
  }
  
  game_ns._draw_setup.select_on_player_color_change = function() {
  	var colors_taken = [];
  
  	var wrap = $(this).closest('.player-wrap');
  
  	// assume current and before as static
  	colors_taken.push($(this).val());
  
  	// change any next to any other color
  	wrap.siblings().each(function(index, el) {
  		var el2 = $(el).find('.player-color');
  		var val2 = el2.val();
  		var is_already_present = colors_taken.indexOf(val2) != -1;
  		if (is_already_present) {
  			// change its color to next available
  			var colors_not_taken = arr_diff(game_ns.available_colors, colors_taken);
  			el2.val(colors_not_taken[0]);
  		}
  
  		// refresh val2
  		val2 = el2.val();
  		colors_taken.push(val2);
  
  	});
  
  }
  
  game_ns.setup = function() {
  
  	pcount = parseInt(document.getElementById("playernumber").value, 10);
  
  	var playerArray = new Array(pcount);
  	var p, wrap, intel;
  
  	playerArray.randomize();
  
  	for (var i = 1; i <= pcount; i++) {
  		p = player[playerArray[i - 1]];
  
  		wrap = $("div#player"+i+"wrap");
  		intel = wrap.find("select.player-intel").val();
  
  		p.color = wrap.find("select.player-color").val().toLowerCase();
  
  		if (intel === "0") {
  			p.name = wrap.find("input.player-name").val();
  			p.human = true;
  		} else {
  			p.human = false;
  			p.AI = new AITest(p);
  		}
  	}
  
  	$("#board, #moneybar").show();
  	$("#setup").hide();
  
  	if (pcount === 2) {
  		document.getElementById("stats").style.width = "454px";
  	} else if (pcount === 3) {
  		document.getElementById("stats").style.width = "686px";
  	}
  
  	document.getElementById("stats").style.top = "0px";
  	document.getElementById("stats").style.left = "0px";
  
  	play();
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  // function togglecheck(elementid) {
  	// element = document.getElementById(elementid);
  
  	// if (window.event.srcElement.id == elementid)
  		// return;
  
  	// if (element.checked) {
  		// element.checked = false;
  	// } else {
  		// element.checked = true;
  	// }
  // }
  
  function getCheckedProperty() {
  	for (var i = 0; i < 42; i++) {
  		if (document.getElementById("propertycheckbox" + i) && document.getElementById("propertycheckbox" + i).checked) {
  			return i;
  		}
  	}
  	return -1; // No property is checked.
  }
  
  // function propertycell_onclick(element, num) {
  	// togglecheck("propertycheckbox" + num);
  	// if (document.getElementById("propertycheckbox" + num).checked) {
  
  		// // Uncheck all other boxes.
  		// for (var i = 0; i < 40; i++) {
  			// if (i !== num && document.getElementById("propertycheckbox" + i)) {
  				// document.getElementById("propertycheckbox" + i).checked = false;
  			// }
  		// }
  	// }
  
  	// updateOption();
  // }
  
  function menuitem_onmouseover(element) {
  	element.className = "menuitem menuitem_hover";
  	return;
  }
  
  function menuitem_onmouseout(element) {
  	element.className = "menuitem";
  	return;
  }
  
  window.onload = function() {
  	game_ns.draw_setup();
  
  	game = new Game();
  
  	for (var i = 0; i <= 8; i++) {
  		player[i] = new Player("", "");
  		player[i].index = i;
  	}
  
  	var groupPropertyArray = [];
  	var groupNumber;
  
  	for (var i = 0; i < 40; i++) {
  		groupNumber = square[i].groupNumber;
  
  		if (groupNumber > 0) {
  			if (!groupPropertyArray[groupNumber]) {
  				groupPropertyArray[groupNumber] = [];
  			}
  
  			groupPropertyArray[groupNumber].push(i);
  		}
  	}
  
  	for (var i = 0; i < 40; i++) {
  		groupNumber = square[i].groupNumber;
  
  		if (groupNumber > 0) {
  			square[i].group = groupPropertyArray[groupNumber];
  		}
  
  		square[i].index = i;
  	}
  
  	AITest.count = 0;
  
  	player[1].human = true;
  	player[0].name = "the bank";
  
  	communityChestCards.index = 0;
  	chanceCards.index = 0;
  
  	communityChestCards.deck = [];
  	chanceCards.deck = [];
  
  	for (var i = 0; i < 16; i++) {
  		chanceCards.deck[i] = i;
  		communityChestCards.deck[i] = i;
  	}
  
  	// Shuffle Chance and Community Chest decks.
  	chanceCards.deck.sort(function() {return Math.random() - 0.5;});
  	communityChestCards.deck.sort(function() {return Math.random() - 0.5;});
  
  	$("#nextbutton").click(game.next);
  	$("#noscript").hide();
  	$("#setup, #noF5").show();
  
  	var enlargeWrap = document.body.appendChild(document.createElement("div"));
  
  	enlargeWrap.id = "enlarge-wrap";
  
  	var HTML = "";
  	for (var i = 0; i < 40; i++) {
  		HTML += "<div id='enlarge" + i + "' class='enlarge'>";
  		HTML += "<div id='enlarge" + i + "color' class='enlarge-color'></div><br /><div id='enlarge" + i + "name' class='enlarge-name'></div>";
  		HTML += "<br /><div id='enlarge" + i + "price' class='enlarge-price'></div>";
  		HTML += "<br /><div id='enlarge" + i + "token' class='enlarge-token'></div></div>";
  	}
  
  	enlargeWrap.innerHTML = HTML;
  
  	var currentCell;
  	var currentCellAnchor;
  	var currentCellPositionHolder;
  	var currentCellName;
  	var currentCellOwner;
  
  	for (var i = 0; i < 40; i++) {
  		s = square[i];
  
  		currentCell = document.getElementById("cell" + i);
  
  		currentCellAnchor = currentCell.appendChild(document.createElement("div"));
  		currentCellAnchor.id = "cell" + i + "anchor";
  		currentCellAnchor.className = "cell-anchor";
  
  		currentCellPositionHolder = currentCellAnchor.appendChild(document.createElement("div"));
  		currentCellPositionHolder.id = "cell" + i + "positionholder";
  		currentCellPositionHolder.className = "cell-position-holder";
  		currentCellPositionHolder.enlargeId = "enlarge" + i;
  
  		currentCellName = currentCellAnchor.appendChild(document.createElement("div"));
  		currentCellName.id = "cell" + i + "name";
  		currentCellName.className = "cell-name";
  		currentCellName.textContent = s.name;
  
  		if (square[i].groupNumber) {
  			currentCellOwner = currentCellAnchor.appendChild(document.createElement("div"));
  			currentCellOwner.id = "cell" + i + "owner";
  			currentCellOwner.className = "cell-owner";
  		}
  
  		document.getElementById("enlarge" + i + "color").style.backgroundColor = s.color;
  		document.getElementById("enlarge" + i + "name").textContent = s.name;
  		document.getElementById("enlarge" + i + "price").textContent = s.pricetext;
  	}
  
  
  	// Add images to enlarges.
  	document.getElementById("enlarge0token").innerHTML += '<img src="images/arrow_icon.png" height="40" width="136" alt="" />';
  	document.getElementById("enlarge20price").innerHTML += "<img src='images/free_parking_icon.png' height='80' width='72' alt='' style='position: relative; top: -20px;' />";
  	document.getElementById("enlarge38token").innerHTML += '<img src="images/tax_icon.png" height="60" width="70" alt="" style="position: relative; top: -20px;" />';
  
  	corrections();
  
  	// Jail corrections
  	$("<div>", {id: "jailpositionholder" }).appendTo("#jail");
  	$("<span>").text("Jail").appendTo("#jail");
  
  	document.getElementById("jail").enlargeId = "enlarge40";
  
  	document.getElementById("enlarge-wrap").innerHTML += "<div id='enlarge40' class='enlarge'><div id='enlarge40color' class='enlarge-color'></div><br /><div id='enlarge40name' class='enlarge-name'>Jail</div><br /><div id='enlarge40price' class='enlarge-price'><img src='images/jake_icon.png' height='80' width='80' alt='' style='position: relative; top: -20px;' /></div><br /><div id='enlarge40token' class='enlarge-token'></div></div>";
  
  	document.getElementById("enlarge40name").innerHTML = "Jail";
  
  	// Create event handlers for hovering and draging.
  
  	var drag, dragX, dragY, dragObj, dragTop, dragLeft;
  
  	$(".cell-position-holder, #jail").on("mouseover", function(){
  		$("#" + this.enlargeId).show();
  
  	}).on("mouseout", function() {
  		$("#" + this.enlargeId).hide();
  
  	}).on("mousemove", function(e) {
  		var element = document.getElementById(this.enlargeId);
  
  		if (e.clientY + 20 > window.innerHeight - 204) {
  			element.style.top = (window.innerHeight - 204) + "px";
  		} else {
  			element.style.top = (e.clientY + 20) + "px";
  		}
  
  		element.style.left = (e.clientX + 10) + "px";
  	});
  
  
  	$("body").on("mousemove", function(e) {
  		var object;
  
  		if (e.target) {
  			object = e.target;
  		} else if (window.event && window.event.srcElement) {
  			object = window.event.srcElement;
  		}
  
  
  		if (object.classList.contains("propertycellcolor") || object.classList.contains("statscellcolor")) {
  			if (e.clientY + 20 > window.innerHeight - 279) {
  				document.getElementById("deed").style.top = (window.innerHeight - 279) + "px";
  			} else {
  				document.getElementById("deed").style.top = (e.clientY + 20) + "px";
  			}
  			document.getElementById("deed").style.left = (e.clientX + 10) + "px";
  
  
  		} else if (drag) {
  			if (e) {
  				dragObj.style.left = (dragLeft + e.clientX - dragX) + "px";
  				dragObj.style.top = (dragTop + e.clientY - dragY) + "px";
  
  			} else if (window.event) {
  				dragObj.style.left = (dragLeft + window.event.clientX - dragX) + "px";
  				dragObj.style.top = (dragTop + window.event.clientY - dragY) + "px";
  			}
  		}
  	});
  
  
  	$("body").on("mouseup", function() {
  
  		drag = false;
  	});
  	document.getElementById("statsdrag").onmousedown = function(e) {
  		dragObj = document.getElementById("stats");
  		dragObj.style.position = "relative";
  
  		dragTop = parseInt(dragObj.style.top, 10) || 0;
  		dragLeft = parseInt(dragObj.style.left, 10) || 0;
  
  		if (window.event) {
  			dragX = window.event.clientX;
  			dragY = window.event.clientY;
  		} else if (e) {
  			dragX = e.clientX;
  			dragY = e.clientY;
  		}
  
  		drag = true;
  	};
  
  	document.getElementById("popupdrag").onmousedown = function(e) {
  		dragObj = document.getElementById("popup");
  		dragObj.style.position = "relative";
  
  		dragTop = parseInt(dragObj.style.top, 10) || 0;
  		dragLeft = parseInt(dragObj.style.left, 10) || 0;
  
  		if (window.event) {
  			dragX = window.event.clientX;
  			dragY = window.event.clientY;
  		} else if (e) {
  			dragX = e.clientX;
  			dragY = e.clientY;
  		}
  
  		drag = true;
  	};
  
  	$("#mortgagebutton").click(function() {
  		var checkedProperty = getCheckedProperty();
  		var s = square[checkedProperty];
  
  		if (s.mortgage) {
  			if (player[s.owner].money < Math.round(s.price * 0.6)) {
  				popup("<p>You need $" + (Math.round(s.price * 0.6) - player[s.owner].money) + " more to unmortgage " + s.name + ".</p>");
  
  			} else {
  				popup("<p>" + player[s.owner].name + ", are you sure you want to unmortgage " + s.name + " for $" + Math.round(s.price * 0.6) + "?</p>", function() {
  					unmortgage(checkedProperty);
  				}, "Yes/No");
  			}
  		} else {
  			popup("<p>" + player[s.owner].name + ", are you sure you want to mortgage " + s.name + " for $" + Math.round(s.price * 0.5) + "?</p>", function() {
  				mortgage(checkedProperty);
  			}, "Yes/No");
  		}
  
  	});
  
  	$("#buyhousebutton").on("click", function() {
  		var checkedProperty = getCheckedProperty();
  		var s = square[checkedProperty];
  		var p = player[s.owner];
  		var houseSum = 0;
  		var hotelSum = 0;
  
  		if (p.money < s.houseprice) {
  			if (s.house === 4) {
  				popup("<p>You need $" + (s.houseprice - player[s.owner].money) + " more to buy a hotel for " + s.name + ".</p>");
  				return;
  			} else {
  				popup("<p>You need $" + (s.houseprice - player[s.owner].money) + " more to buy a house for " + s.name + ".</p>");
  				return;
  			}
  		}
  
  		for (var i = 0; i < 40; i++) {
  			if (square[i].hotel === 1) {
  				hotelSum++;
  			} else {
  				houseSum += square[i].house;
  			}
  		}
  
  		if (s.house < 4 && houseSum >= 32) {
  			popup("<p>All 32 houses are owned. You must wait until one becomes available.</p>");
  			return;
  		} else if (s.house === 4 && hotelSum >= 12) {
  			popup("<p>All 12 hotels are owned. You must wait until one becomes available.</p>");
  			return;
  		}
  
  		buyHouse(checkedProperty);
  
  	});
  
  	$("#sellhousebutton").click(function() { sellHouse(getCheckedProperty()); });
  
  	$("#viewstats").on("click", showStats);
  	$("#statsclose, #statsbackground").on("click", function() {
  		$("#statswrap").hide();
  		$("#statsbackground").fadeOut(400);
  	});
  
  	$("#buy-menu-item").click(function() {
  		$("#buy").show();
  		$("#manage").hide();
  
  		// Scroll alerts to bottom.
  		$("#alert").scrollTop($("#alert").prop("scrollHeight"));
  	});
  
  
  	$("#manage-menu-item").click(function() {
  		$("#manage").show();
  		$("#buy").hide();
  	});
  
  
  	$("#trade-menu-item").click(game.trade);
  
  
  };
  
  
  
  function arr_diff(a1, a2) {
      var a = [], diff = [];
  
      for (var i = 0; i < a1.length; i++) {
          a[a1[i]] = true;
      }
  
      for (var i = 0; i < a2.length; i++) {
          if (a[a2[i]]) {
              delete a[a2[i]];
          } else {
              a[a2[i]] = true;
          }
      }
  
      for (var k in a) {
          diff.push(k);
      }
  
      return diff;
  };
  