text: var skipuse = 0;
  var skiplevel = -1;
  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;
  		// die1=4;
  		// die2=4;
  		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, and now will be eliminated from the game." + ".</p>", game.eliminatePlayer());
  			} else {
  				roll();
  			}
  		} else if (areDiceRolled) {
  			play();
  		} 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;
  	};
  
  
  	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.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 < 24; i++) {
  			if(square[i].owner === p.index)
  			{
  				square[i].level=0;
  				square[i].owner=0;
  				square[i].updateSquare();
  			}
  
  			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();
  
  
  			// // 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>";
  			
  			$("#board").hide();
  			
  			popup("<p>Congratulations, " + player[1].name + ", you have won the game.</p><div>");
  			$("#control").hide();
  			
  			$("#refresh").show();
  
  		} else {
  			popup("<p>" + player[1].name + "has been eliminated for having less than 0 points.</p><div>");
  			play();
  		}
  	};
  
  	this.bankruptcyUnmortgage = function() {
  		var p = player[turn];
  
  		if (p.creditor === 0) {
  			game.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.");
  		updateMoney();
  		game.eliminatePlayer();
  
  	};
  
  }
  
  var game;
  
  function Player(name, color) {
  	this.name = name;
  	this.color = color;
  	this.freehit=0;
  	this.usefreehit=0;
  	this.blockcard=0;
  	this.skipcard = 0;
  	this.position = 0;
  	this.money = 60;
  	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;
  		}
  	};
  }
  
  
  
  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 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;
  	}
  
  	// Yes/No
  	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 !== "blank") {
  		$("#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);
  
  	}
  
  	// Show using animation.
  	$("#popupbackground").fadeIn(400, function() {
  		$("#popupwrap").show();
  	});
  
  }
  
  
  function updatePosition() {
  	// console.log("hai");
  	// Reset borders
  	// document.getElementById("jail").style.border = "1px solid black";
  	// document.getElementById("jailpositionholder").innerHTML = "";
  	for (var i = 0; i < 24; 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 < 24; x++) {
  		sq = square[x];
  		left = 0;
  		top = 0;
  
  		for (var y = turn; y <= pcount; y++) {
  
  			if (player[y].position == x) {
  
  				// console.log("hai enter");
  				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];
  		var grade;
  		if(p_i.money<60)
  			grade="F";
  		else if(p_i.money <80)
  			grade="D";
  		else if(p_i.money <100)
  			grade="C-";
  		else if(p_i.money <120)
  			grade="C";
  		else if(p_i.money < 140)
  			grade="B-";
  		else if(p_i.money <160)
  			grade="B";
  		else if( p_i.money < 180)
  			grade="A-";
  		else
  			grade="A";
  
  		$("#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;
  		document.getElementById("p" + i + "grade").innerHTML = "Grade: " + grade;
  	}
  	// 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) {
  		// $("#resignbutton").show();
  		// $("#nextbutton").hide();
  
  		game.eliminatePlayer();
  		
  	} 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 = "static/Die_" + die0 + ".png";
  		element0.alt = die0;
  
  		if (element1.firstChild) {
  			element1 = element1.firstChild;
  		} else {
  			element1 = element1.appendChild(document.createElement("img"));
  		}
  
  		element1.src = "static/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 < 24; i++) {
  		sq = square[i];
  		if (sq.owner === 0) {
  			$("#cell" + i + "owner").hide();
  		} else if (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 < 24; i++) {
  		sq = square[i];
  		if (sq.owner == turn) {
  
  
  			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>";
  	}
  
  
  }
  
  function updateOption() {
  	$("#option").show();
  
  
  }
  
  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='static/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='static/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 < 24; 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='static/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='static/house.png' alt='' title='House' class='house' style='float: none;' /></span>";
  				}
  
  				HTML += "<tr><td class='statscellcolor' style='background: " + color[sq.level-1] + ";";
  
  				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 cards.";
  		} 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 = color[sq.level-1];
  			document.getElementById("deed-name").textContent = sq.name;
  			document.getElementById("difficulty-level").textContent = stages[sq.level-1];
  			document.getElementById("deed-points").textContent = reward[sq.level-1];
  			
  		
  		// }
  	
  }
  
  function hidedeed() {
  	$("#deed").hide();
  }
  
  function blocksquarebutton()
  {
  	var p = player[turn];
  	var property = square[p.position];
  	property.blocked=1;
  	p.blockcard--;
  }
  
  function ajaxtest()
  {
  	$.ajax({
  	type: "POST",
  	url: "/run",
  	data: { param: ""}
  	}).done(function( data ) {
  		console.log(data);
  	});
  }
  
  
  
  function buy() 
  {
  	
  	var p = player[turn];
  	var property = square[p.position];
  	var cost = property.price;
  
  	if(property.blocked ===1 && p.index!=property.owner)
  	{
  		addAlert(property.owner + " has used a Block Square card on this question, hard luck! ");
  		property.blocked--;
  		updateMoney();
  		updateOwned();
  	}
  
  	else if(skipuse === 1)
  	{
  		// var result = Math.floor(Math.random() * 2);
  		//var result = 0;
  		$.ajax({
                type:'POST',
                url: "/foo",
                contentType: "application/json",
                data: JSON.stringify({"level":skiplevel}),
                success: function(response){
                  console.log(response.message);
  				//  alert(response);
  				// var result = JSON.parse(response);
  				// console.log(result.message);
  				if(response.message === 1)
  				{
  					if(property.owner)
  					{
  						var temp = player[property.owner];
  						temp.money -= reward[property.level-1];
  						addAlert(temp.name + " loses " + reward[property.level-1] + " points, from " + property.name);
  					}
  					addAlert(p.name + " solved " + property.name + " on level " + stages[skiplevel] +" and received " + reward[skiplevel] + " points.");
  					p.money += reward[skiplevel];
  					property.level = skiplevel + 1;
  					property.updateSquare();
  					property.owner = turn;		
  				}
  				else
  				{
  					if(p.usefreehit === 1)
  						addAlert(p.name + " failed to solve " + property.name + " on level " + stages[property.level] +" , but uses free hit card to avoid penalty ");	
  					else
  					{
  						addAlert(p.name + " failed to solve " + property.name + " on level " + stages[skiplevel] +" and lost " + reward[skiplevel]/4 + " points.");
  						p.money -= reward[skiplevel]/4;
  					}
  				}
  				skipuse = -1;
  				skiplevel = -1;
  				updateMoney();
  				updateOwned();
  				p.usefreehit=0;
  				game.next();
  				// return result_py;
                }
        	});
  	}
  
  	else
  	{
  		// var result = Math.floor(Math.random() * 2);
  		//var result = 0;
  		$.ajax({
                type:'POST',
                url: "/foo",
  			  contentType: "application/json",
                data: JSON.stringify({"level":property.level}),
                success: function(response){
                  //  console.log(response);
  				//  alert(response);
  				// var result = JSON.parse(response);
  				// console.log(result.message);
  				if(response.message === 1)
  				{
  					if(property.owner)
  					{
  						var temp = player[property.owner];
  						temp.money -= reward[property.level-1];
  						addAlert(temp.name + " loses " + reward[property.level-1] + " points, from " + property.name);
  					}
  					addAlert(p.name + " solved " + property.name + " on level " + stages[property.level] +" and received " + property.pricetext + " points.");
  					p.money += reward[property.level];
  					property.level++;
  					property.updateSquare();
  					property.owner = turn;		
  				}
  				else
  				{
  					if(p.usefreehit === 1)
  						addAlert(p.name + " failed to solve " + property.name + " on level " + stages[property.level] +" , but uses free hit card to avoid penalty ");	
  					else
  					{
  						addAlert(p.name + " failed to solve " + property.name + " on level " + stages[property.level] +" and lost " + reward[property.level]/4 + " points.");
  						p.money -= reward[property.level]/4;
  					}
  				}
  				updateMoney();
  				updateOwned();
  				p.usefreehit=0;
  				game.next();
  				// return result_py;
                }
        	});
  		
  	}
  	
  
  }
  
  
  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 pass()
  {
  	var p = player[turn];
  	var s = square[p.position];
  	if(s.owner != 0)
  	{
  		var rent = reward[s.level-1]/8;
  		if(s.owner != turn)
  			addAlert(p.name + " paid " + rent + " points as rent to " + player[s.owner].name + ".");
  		p.money -= rent;
  		player[s.owner].money += rent;
  		updateMoney();
  		updatePosition();
  	}
  	game.next();
  }
  
  function freehitbutton()
  {
  	var p = player[turn];
  	p.usefreehit=1;
  	p.freehit--;
  	solve();
  }
  
  function skipmed()
  {
  	skipuse = 1;
  	skiplevel = 1;
  	solve();
  }
  
  function skiphard()
  {
  	skipuse = 1;
  	skiplevel = 2;
  	solve();
  }
  
  function skipbutton()
  {
  	var p = player[turn];
  	var sq = square[p.position];
  	p.skipcard--;
  	document.getElementById("landed").innerHTML = "";
  	if(sq.level < 1)
  		document.getElementById("landed").innerHTML += "<div> Skip to Medium Level question <a href='javascript:void(0);'   class='statscellcolor'>" + "</a>.<input type='button' onclick='skipmed();' value='Skip to Medium ' title='Skip Medium " + ".'/></div>";
  	if(sq.level < 2)
  		document.getElementById("landed").innerHTML += "<div> Skip to Hard Level question <a href='javascript:void(0);'   class='statscellcolor'>" + "</a>.<input type='button' onclick='skiphard();' value='Skip to Hard ' title='Skip Hard " + ".'/></div>";
  	document.getElementById("landed").innerHTML += "<div><input type='button' onclick='buy();' value='Verify Result ' title='Verify Result " + s.name + ".'/></div>";
  		
  }
  
  function solve()
  {
  	var p = player[turn];
  	var property = square[p.position];
  	var question_no = property.qno;
  	if(property.blocked ===1 && p.index!=property.owner)
  	{
  		addAlert(property.owner + " has used a Block Square card on this question, hard luck! ");
  		property.blocked--;
  		updateMoney();
  		updateOwned();
  		game.next();
  	}
  	else if(skipuse===1){
  	$("#nextbutton").hide();
  	document.getElementById("landed").innerHTML = "<div><input type='button' onclick='buy();' value='Verify Result ' title='Verify Result " + s.name + ".'/></div>";
  	$.ajax({
                type:'POST',
  			  url: "/bar",
  			//   data: {param: yoyo},
  			//   dataType:"text",
  			  contentType: "application/json",
                data: JSON.stringify({"qno": question_no,"level":skiplevel}),
                success: function(response){
                  //  console.log(response);
  				//  alert(response);
  				// var result = JSON.parse(response);
  				console.log(response);
  				// return result_py;
                }
  		  });
  		  	
  	}
  	else
  	{
  		$("#nextbutton").hide();
  		document.getElementById("landed").innerHTML = "<div><input type='button' onclick='buy();' value='Verify Result ' title='Verify Result " + s.name + ".'/></div>";
  		$.ajax({
  			type:'POST',
  			url: "/bar",
  		  //   data: {param: yoyo},
  		  //   dataType:"text",
  			contentType: "application/json",
  			data: JSON.stringify({"qno": question_no,"level":property.level}),
  			success: function(response){
  			  //  console.log(response);
  			  //  alert(response);
  			  // var result = JSON.parse(response);
  			  console.log(response);
  			  // return result_py;
  			}
  		});	
  	}
  }
  
  function land(increasedRent) {
  	$("#nextbutton").hide();
  	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();
  	if(p.position ===0 || p.position ===6 || p.position ===12 || p.position ===18)
  		document.getElementById("landed").innerHTML = "You landed on " + s.name + ".";
  	else
  		document.getElementById("landed").innerHTML = "";
  	s.landcount++;
  	addAlert(p.name + " landed on " + s.name + ".");
  	if(p.index===s.owner && p.blockcard>0){
  		//console.log("hi");
  		document.getElementById("landed").innerHTML = "<div> You have " + p.blockcard +" available Block Square cards <a href='javascript:void(0);'   class='statscellcolor'>" + "</a>.<input type='button' onclick='blocksquarebutton();' value='Use Block Square' title='Block Square " + ".'/></div>";
  	}
  	// Allow player to buy the property on which he landed.
  	if(p.position!=0 && p.position!=6 && p.position!=12 && p.position != 18){
  	if (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='solve();' value='Solve for (" + s.price + ")' title='Solve " + s.name + " for " + s.pricetext + ".'/></div>";
  			document.getElementById("landed").innerHTML += "<div><input type='button' onclick='pass();' value='Pass  ' title='Pass " + s.name + ".'/></div>";
  			if (p.freehit>0)
  				document.getElementById("landed").innerHTML += "<div> You have " +p.freehit+ " available free hit cards <a href='javascript:void(0);'   class='statscellcolor'>" + "</a>.<input type='button' onclick='freehitbutton();' value='Use Free Hit ' title='Free Hit " + ".'/></div>";
  			if(p.skipcard > 0 && s.level < 2)
  				document.getElementById("landed").innerHTML += "<div> You have " +p.skipcard+ " available skip cards <a href='javascript:void(0);'   class='statscellcolor'>" + "</a>.<input type='button' onclick='skipbutton();' value='Use Skip Card ' title='Skip " + ".'/></div>";
  		// }
  
  
  		// game.addPropertyToAuctionQueue(p.position);
  	}
  
  	// Collect rent
  	if (s.owner !== 0 && s.owner != turn) 
  	{
  		var rent;
  
  
  		if(s.level === 3)
  		{
  			rent = 12/8;
  			addAlert(p.name + " paid " + rent + " points as rent to " + player[s.owner].name + ".");
  			p.money -= rent;
  			player[s.owner].money += rent;
  		}
  		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='solve();' value='Solve for (" + s.price + ")' title='Solve " + s.name + " for " + s.pricetext + ".'/></div>";
  			document.getElementById("landed").innerHTML += "<div><input type='button' onclick='pass();' value='Pass and pay (" + reward[s.level-1]/8 + ")' title='Pass " + s.name + " for " + reward[s.level-1]/8 + ".'/></div>";
  			if (p.freehit>0)
  				document.getElementById("landed").innerHTML += "<div> You have " + p.freehit +" available free hit cards <a href='javascript:void(0);'   class='statscellcolor'>" + "</a>.<input type='button' onclick='freehitbutton();' value='Use Free Hit ' title='Free Hit " + ".'/></div>";
  
  			if(p.skipcard > 0 && s.level < 2)
  				document.getElementById("landed").innerHTML += "<div> You have " +p.skipcard+ " available skip cards <a href='javascript:void(0);'   class='statscellcolor'>" + "</a>.<input type='button' onclick='skipbutton();' value='Use Skip Card ' title='Skip " + ".'/></div>";
  
  		}
  
  		
  	} 
  	else if (s.owner != 0 && s.owner === turn && p.level!=3)
  	{
  		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='solve();' value='Solve for (" + s.price + ")' title='Solve " + s.name + " for " + s.pricetext + ".'/></div>";
  		document.getElementById("landed").innerHTML += "<div><input type='button' onclick='pass();' value='Pass' title='Pass " + s.name  +  ".'/></div>";
  		if (p.freehit>0)
  			document.getElementById("landed").innerHTML += "<div> You have " + p.freehit +" available free hit cards <a href='javascript:void(0);'   class='statscellcolor'>" + "</a>.<input type='button' onclick='freehitbutton();' value='Use Free Hit ' title='Free Hit " + ".'/></div>";
  
  		if(p.skipcard > 0 && s.level < 2)
  			document.getElementById("landed").innerHTML += "<div> You have " +p.skipcard+ " available skip cards <a href='javascript:void(0);'   class='statscellcolor'>" + "</a>.<input type='button' onclick='skipbutton();' value='Use Skip Card ' title='Skip " + ".'/></div>";
  	}
  }
  
  else if (p.position === 6)
  {
  	addAlert(p.name + " receives a Free Hit card! Use it to attempt any question without penalties");
  	p.freehit++;
  }
  else if (p.position === 12)
  {
  	addAlert(p.name + " receives a Block Square card! Use it to block an attempt from the next person landing on the square that you solve");
  	p.blockcard++;
  }
  else if(p.position === 18)
  {
  	addAlert(p.name + " receives a Skip Levels card! Use it to attempt a question without having to solve its easier versions");
  	p.skipcard++;
  }
  	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);
  
  
  	
  		addAlert(p.name + " rolled " + (die1 + die2) + ".");
  	
  	
  		document.getElementById("nextbutton").value = "End turn";
  		document.getElementById("nextbutton").title = "End turn and advance to the next player.";
  		doublecount = 0;
  
  	updatePosition();
  	updateMoney();
  	updateOwned();
  
  		updateDice(die1, die2);
  
  		// Move player
  		p.position += die1 + die2;
  
  		// Collect $200 salary as you pass GO
  		if (p.position >= 24) {
  			p.position -= 24;
  			p.money += 2;
  			addAlert(p.name + " received 2 points 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();
  		}
  	}
  }
  
  function setup() {
  	pcount = parseInt(document.getElementById("playernumber").value, 10);
  
  	var playerArray = new Array(pcount);
  	var p;
  
  	playerArray.randomize();
  
  	for (var i = 1; i <= pcount; i++) {
  		p = player[playerArray[i - 1]];
  
  
  		p.color = document.getElementById("player" + i + "color").value.toLowerCase();
  
  		// if (document.getElementById("player" + i + "ai").value === "0") {
  			p.name = document.getElementById("player" + i + "name").value;
  			p.human = true;
  		// } else if (document.getElementById("player" + i + "ai").value === "1") {
  		// 	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 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 playernumber_onchange() {
  	pcount = parseInt(document.getElementById("playernumber").value, 10);
  
  	$(".player-input").hide();
  
  	for (var i = 1; i <= pcount; i++) {
  		$("#player" + i + "input").show();
  	}
  }
  
  function menuitem_onmouseover(element) {
  	element.className = "menuitem menuitem_hover";
  	return;
  }
  
  function menuitem_onmouseout(element) {
  	element.className = "menuitem";
  	return;
  }
  
  window.onload = function() {
  	game = new Game();
  
  	for (var i = 0; i <= 8; i++) {
  		player[i] = new Player("", "");
  		player[i].index = i;
  	}
  
  
  
  	for (var i = 0; i < 24; i++) {
  		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;});
  
  	$("#playernumber").on("change", playernumber_onchange);
  	playernumber_onchange();
  
  	$("#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 < 24; 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 < 24; i++) {
  		s = square[i];
  
  		currentCell = document.getElementById("cell" + i);
  		
  
  		currentCellAnchor = currentCell.appendChild(document.createElement("div"));
  		// console.log(i);
  		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;
  
  		
  			currentCellOwner = currentCellAnchor.appendChild(document.createElement("div"));
  			currentCellOwner.id = "cell" + i + "owner";
  			currentCellOwner.className = "cell-owner";
  		
  		if(i===0 || i===6 || i===12 || i===18)
  			document.getElementById("enlarge" + i + "color").style.backgroundColor = color[3];
  		else
  			document.getElementById("enlarge" + i + "color").style.backgroundColor = color[s.level];
  		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="static/arrow_icon.png" height="40" width="136" alt="" />';
  	document.getElementById("enlarge20price").innerHTML += "<img src='static/free_parking_icon.png' height='80' width='72' alt='' style='position: relative; top: -20px;' />";
  	// document.getElementById("enlarge38token").innerHTML += '<img src="static/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='static/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(){
  
  		// document.getElementById(this.enlargeId + "color").style.backgroundColor = color[s.level];
  		// document.getElementById(this.enlargeId + "name").textContent = s.name;
  		// document.getElementById(this.enlargeId + "price").textContent = s.pricetext;
  		for (var i = 0; i < 24; i++)
  		{
  			var s = square[i];
  			if(i===0 || i===6 || i===12 || i===18)
  			document.getElementById("enlarge" + i + "color").style.backgroundColor = color[3];
  			else
  				document.getElementById("enlarge" + i + "color").style.backgroundColor = color[s.level];
  			document.getElementById("enlarge" + i + "name").textContent = s.name;
  			document.getElementById("enlarge" + i + "price").textContent = s.pricetext;
  		}	
  		$("#" + 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);
  
  
  };
  