/*

Create Players

Functions required to create player records

ie bet placing, names, new players etc

*/

if (debug) { alert('players loaded'); }


function player(name,money) {
	this.name = name;
	this.money = money;
	this.bet = 0;
	this.horse = 0;
}

function create_players(ppl) {

	for (var p = 1; p <= ppl; p++) {
		players[p] = new player("Player_"+p,start_money);
	}

}

function do_players(form) {
	for (var p = 1; p <= this.ppl_playing; p++) {
		players[p] = new player(eval("form.player_name_"+p+".value"),start_money);
	}
	make_dough();
	location='horse.html';
}

function Delete_Old_Data() {
	for (var p = 1; p <= this.ppl_playing; p++) {
		var cookie_name = "horse_player_"+p+"_money";
		DeleteCookie (cookie_name);
		var cookie_name = "horse_player_"+p;
		DeleteCookie (cookie_name);
	}
	var cookie_name = "horse_players";
	DeleteCookie (cookie_name);
}

function force_new_game() {
	new_game();
	make_dough();
	location='horse.html';
}

function new_game() {

	// need to be able to change player numbers and details here.
	//this.ppl_playing = prompt('How many people are playing?',this.ppl_playing);
	//showbox(3);
	Delete_Old_Data();
	ppl_playing = 1;
	players = new Array(this.ppl_playing);
	create_players(ppl_playing);
	
}

function SavePlayerDetails() {
	
	for (var p = 1; p <= this.ppl_playing; p++) {
		players[p].name = document.getElementById("player_"+p+"_name").value;
		//Change player betting names
		document.getElementById("player_"+p+"_bet_name").innerText = players[p].name;
	}
	make_dough();
	hidebox(4);
}

function do_bets(form) {

	for (var p = 1; p <= this.ppl_playing; p++) {
		players[p].bet = eval("form.bet_"+p).value;
		players[p].horse = eval("form.choice_"+p)[eval("form.choice_"+p).selectedIndex].value;
	}

}

function calc_bets(winner) {

	for (var p = 1; p <= this.ppl_playing; p++) {
		if (players[p].horse == winner) {
			// because we haven't taken the bet off we need to... hence the - bet...
			var winnings = parseInt(players[p].bet) + (parseInt(players[p].bet) * horses[winner].odds_win);
			players[p].money = parseInt(players[p].money) + winnings - parseInt(players[p].bet);
			alert(players[p].name+" Wins: "+winnings);
		} else {
			players[p].money = players[p].money - players[p].bet;
			alert(players[p].name+" Loses "+players[p].bet+" now "+ players[p].money);
		}
	}

}

function make_dough() {

	for (var p = 1; p <= this.ppl_playing; p++) {
		var cookie_name = "horse_player_"+p+"_money";
		SetCookie (cookie_name, players[p].money);
		var cookie_name = "horse_player_"+p;
		SetCookie (cookie_name, players[p].name);
	}
	var cookie_name = "horse_players";
	SetCookie (cookie_name, this.ppl_playing);

}

function get_dough() {

	for (var p = 1; p <= this.ppl_playing; p++) {

		var cookie_name = "horse_player_"+p+"_money";
		var player_money = parseInt(GetCookie(cookie_name));
		cookie_name = "horse_player_"+p;
		var player_name = GetCookie(cookie_name);

		players[p] = new player(player_name,player_money);

	}

}

function check_losers() {

	var losers = 0;
	for (var p = 1; p <= this.ppl_playing; p++) {

		if (players[p].money <= 0) {
			losers++;
		}

	}
	if (losers == this.ppl_playing) {

		if (confirm("All is lost... play again?")) {

			new_game();
			location='horse.html'

		}

	}

}

function AddPlayer() {
	ppl_playing++;
	players[ppl_playing] = new player('Player_'+ppl_playing,start_money);
	players.length=players.length+1;
	make_dough();
	location='horse.html'
}

// SETUP FOR PLAYERS

if (GetCookie("horse_players") != null) {

	var ppl_playing = GetCookie("horse_players");
	var players = new Array(this.ppl_playing);
	get_dough();

} else {

	new_game();

}
