var JS_BINGO = {
	
	game_thread : null,
	player_thread : null,
	
	draw_thread : null,
	
	card_counter : 0,
	counter : 1,
	card_slot : 1,
	bingo_started  : false,
	slot : new Array(false,false,false,false),
	card_num_obj : null,
	alert_flag : false,
	player_waiting_time : 180,
	next_ball_timer : 10,
	card_idx : -1,
	
	//Checking players functions - start
	checkPlayerStatus : function() {
		if(JS_BINGO.player_waiting_time > 0)
			JS_BINGO.player_waiting_time--;
		if(JS_BINGO.player_waiting_time == 0)
			JS_BINGO.stopMonitorPlayers();
		document.getElementById("game_remaining_time").innerHTML = JS_BINGO.player_waiting_time;
		if(JS_BINGO.player_waiting_time == 0) {
			var form_obj = document.bingo_form;
			form_obj.action = "bingo_game_players.php";
			form_obj.option.value = 'check_player_status';
			HTML_AJAX.formSubmit('bingo_form','target_form');
		}
	},
	
	startMonitorPlayers : function(remaining_time) {
		JS_BINGO.player_waiting_time = remaining_time;
		JS_BINGO.player_thread = setInterval('JS_BINGO.checkPlayerStatus()',1000);
		
	},
	
	setGameInfo : function(players,pot) {
		
		var str_info = '<table cellpadding="0" cellspacing="0">';
		str_info += '<td class="txt_14 orange0 pad_5_right">No. of Players:</td>';
		str_info += '<td class="txt_16 bold pad_5_right" width="80">' + players + '<input type="hidden" name="players" value="' + players + '"></td>';
		str_info += '<td class="txt_14 bold orange0 pad_5_right">Pot Prize:</td>';
		str_info += '<td class="txt_16 bold pad_5_right" width="80">$' + pot + '<input type="hidden" name="pot" value="' + pot + '"></td>';
		str_info += '</table>';
		document.getElementById('game_info_td').innerHTML = str_info;		
	},
	refreshPlayerCredit : function(dep_amount) {
		document.getElementById('user_credit_span').innerHTML = dep_amount;
	},
	stopMonitorPlayers : function() {
		 if (JS_BINGO.player_thread != null)
			clearInterval(JS_BINGO.player_thread);
	},
	
	showPlayersPot : function (players,pot,min_pot) {
		
		if(JS_BINGO.player_waiting_time == 0) {
			if (!JS_BINGO.alert_flag) {
				var str = "";
				//var str = "\r\nPlayers: " +  players;
				//str += "\r\nPot: " + pot;
				str += "\r\nMinimum pot prize of $" + min_pot + " has not been reached!";
				str += "\r\n\r\nGame is cancelled. Please try again.";
				JS_BINGO.alert_flag = true;
				JS_BINGO.cancelPlayer();
				alert(str);
				JS_BINGO.alert_flag = false;
			}
		}
	},
	cancelPlayer : function() {
		var form_obj = document.bingo_form;
		form_obj.action = "bingo_game_bet.php";
		form_obj.option.value = 'cancel_player';
		HTML_AJAX.formSubmit('bingo_form','target_form');
		
	},
	//Checking players functions - end
	
	
	//Selecting cards and joining game functions - start
	joinBingoGame : function() {
			
		if (JS_BINGO.card_counter < 1) {
			alert("No card selected!");
			return false;
		}
		var form_obj = document.bingo_form;
		form_obj.action = 'bingo_game_bet.php';
		form_obj.option.value = 'join_game';
		form_obj.card_count.value = JS_BINGO.card_counter;
		HTML_AJAX.formSubmit('bingo_form','target_form');
	},
	toggleSelectCard : function() {
		//document.getElementById("select_card_button").disabled = state;
		//document.getElementById("previous_card_button").disabled = state;
		//document.getElementById("next_card_button").disabled = state;
		if (JS_BINGO.card_idx > 0)
			document.getElementById("previous_card_button").disabled = false;
		else
			document.getElementById("previous_card_button").disabled = true;
		if (JS_BINGO.card_idx < 99)
			document.getElementById("next_card_button").disabled = false;
		else
			document.getElementById("next_card_button").disabled = true;
		
	},
	getNextCard : function() {
		/*JS_BINGO.toggleSelectCard(true);
		var form_obj = document.bingo_form;
		form_obj.action = 'bingo_game_bet.php';
		form_obj.option.value = 'get_next_card';
		HTML_AJAX.formSubmit('bingo_form','bingo_card_selector');*/
		//var card_idx = document.getElementById('card_sequence_no').value;
		//alert(card_idx);
		JS_BINGO.card_idx++;
		JS_BINGO.toggleSelectCard();			
		var sel_con = document.getElementById('bingo_card_selector');
		var sel_card = document.getElementById('bingo_card_div_' + JS_BINGO.card_idx);
		sel_con.innerHTML = sel_card.innerHTML;
		
	},
	 getPreviousCard : function() {
		/*JS_BINGO.toggleSelectCard(true);
		var form_obj = document.bingo_form;
		form_obj.action = 'bingo_game_bet.php';
		form_obj.option.value = 'get_prev_card';
		HTML_AJAX.formSubmit('bingo_form','bingo_card_selector');*/
		//var card_idx = document.getElementById('card_sequence_no').value;
		//document.getElementById('card_sequence_no').value = eval(card_idx) - 1
		//card_idx--;
		JS_BINGO.card_idx--;
		JS_BINGO.toggleSelectCard();			
		var sel_con = document.getElementById('bingo_card_selector');
		var sel_card = document.getElementById('bingo_card_div_' + JS_BINGO.card_idx);
		sel_con.innerHTML = sel_card.innerHTML;
		
		
	},
	selectCard : function() {
		if (JS_BINGO.card_counter == 4) {
			alert("Maximum of 4 cards per screen.");
			return false;
		}
		//JS_BINGO.toggleSelectCard(true);
		JS_BINGO.card_counter++;
		for(var i=0; i < 4; i++) {
			if (JS_BINGO.slot[i] == false) {
				JS_BINGO.card_slot = i + 1;
				JS_BINGO.slot[i] = true;
				break;
			}
		}
		var src_card = document.getElementById("bingo_card_selector");
		var target_card = document.getElementById("bingo_card" + JS_BINGO.card_slot);
		document.getElementById("bingo_card_button" + JS_BINGO.card_slot).disabled = false;
		var src_filename = document.getElementById("bingo_card_filename");
		var target_filename = document.getElementById("bingo_card" + JS_BINGO.card_slot + "_filename");
		target_filename.value = src_filename.value;
		target_card.innerHTML = src_card.innerHTML;
		src_card.innerHTML = "&nbsp;";
		JS_BINGO.getNextCard();	
	},
	
	removeCard : function(card_slot) {
		document.getElementById("bingo_card" + card_slot).innerHTML = "&nbsp;";
		document.getElementById("bingo_card_button" + card_slot).disabled = true;
		JS_BINGO.card_counter--;
		JS_BINGO.slot[card_slot - 1] = false;
	},
	confirmToView : function(msg) {
		if (msg)
			alert(msg);
		else
			alert("Game has started.\r\nClick OK to view the game.");
		document.location = "index.php?page=playbingo";
	},
	//Selecting cards and joining game functions - end
		
	
	//Bingo Draw functions - start
	getNextBall : function() {
		
		document.getElementById("next_ball_timer_span").innerHTML = JS_BINGO.next_ball_timer;		
		if (JS_BINGO.next_ball_timer == 0) {
			JS_BINGO.toggleBall(false);
			var form_obj = document.bingo_form;
			form_obj.action = 'bingo_game_draw.php';
			form_obj.option.value = 'next_ball';
			form_obj.sequence_no.value = JS_BINGO.counter;
			HTML_AJAX.formSubmit('bingo_form','target_form');
			JS_BINGO.counter++;
			if (JS_BINGO.counter >= 76) {
				JS_BINGO.stopBingoDraw();							
			}
			JS_BINGO.next_ball_timer = 10;
			setTimeout('JS_BINGO.toggleBall(true)',2000);
		} else
			JS_BINGO.next_ball_timer--;
	},
	startBingoDraw : function() {
		 JS_BINGO.draw_thread = setInterval('JS_BINGO.getNextBall()',1000);
	},	
	stopBingoDraw : function() {
		 if (JS_BINGO.draw_thread != null)
			clearInterval(JS_BINGO.draw_thread);
		 setTimeout('JS_BINGO.GAMEOVER()',5000);
		// JS_BINGO.reset_game();
	},
	setNextBallDisplay : function(next_ball,letter) {
		document.getElementById('next_ball').innerHTML= letter + " - " + next_ball;
		document.getElementById('tally_'+ next_ball).style.background="#00FF00";
		document.getElementById('ball_counter_span').innerHTML = JS_BINGO.counter-1;
	},	
	populateNumbers : function() {
		if (!JS_BINGO.bingo_started) {
			var form_obj = document.bingo_form;
			form_obj.action = 'bingo_game_draw.php';
			form_obj.option.value = 'populate_all';
			HTML_AJAX.formSubmit('bingo_form','target_form');
			JS_BINGO.bingo_started = true;
		} else {
			//alert('game is still ongoing');
		}
	},
	
	plotBall : function(ball,letter) {
		document.getElementById('tally_'+ ball).style.background="#00FF00";
		document.getElementById('next_ball').innerHTML= letter + " - " + ball;
		JS_BINGO.counter++;
	},
	
	reset_game : function() {

		var form_obj = document.bingo_form;
		form_obj.action = 'bingo_game_draw.php';
		form_obj.option.value = 'reset_game';		
		HTML_AJAX.formSubmit('bingo_form','target_form');
		
	},
	 
	reset_balls : function() {

		var form_obj = document.bingo_form;
		form_obj.option.value = 'reset_balls';
		HTML_AJAX.formSubmit('bingo_form','target_form');
		
	},
	BINGO : function() {
		//JS_BINGO.GAMEOVER();
		var form_obj = document.bingo_form;
		form_obj.option.value = 'BINGO';
		form_obj.action = 'bingo_game_winner.php';
		HTML_AJAX.formSubmit('bingo_form','target_form');
		//var str = HTML_AJAX.grab("check_bingo.php");
		//alert(str);
	},
	GAMEOVER : function() {
		
		var form_obj = document.bingo_form;
		form_obj.action = 'gameover.php';
		HTML_AJAX.formSubmit('bingo_form','main_popup_div');
		//window.open("gameover.html","","menubar=0,resizable=0,width=1000,height=800");
	},
	showBingoAlert : function(row_id,flag) {
		if (flag)
			document.getElementById(row_id).innerHTML = 'YOU WON!';
		else
			document.getElementById(row_id).innerHTML = 'NO BINGO';
	},
	toggleBall : function(flag) {
		if (flag)
			document.getElementById("rolling_ball").innerHTML = '<img src="images/balls.gif" />';
		else
			document.getElementById("rolling_ball").innerHTML = '<img src="images/ball2.gif" />';
	},
	//Bingo Draw functions - end
	
	//Bing Game Status functions -start	
	checkGameStatus : function() {
		var form_obj = document.bingo_form;
		form_obj.action = 'bingo_game_status.php';
		form_obj.option.value = 'check_game_status';
		HTML_AJAX.formSubmit('bingo_form','target_form');
		//alert('checking game');
	},
	startMonitorGame : function() {
		 JS_BINGO.checkGameStatus();
		 JS_BINGO.game_thread = setInterval('JS_BINGO.checkGameStatus()',5000);
	},
	stopMonitorGame : function() {
		 if (JS_BINGO.game_thread != null)
			clearInterval(JS_BINGO.game_thread);
	},
	
	markCardNumber : function(obj) {
		if (!JS_BINGO.bingo_started)
			return false;
		var form_obj = document.bingo_form;
		card_num_obj = obj;
		form_obj.action = 'bingo_game_draw.php';
		form_obj.option.value = 'mark_card';
		form_obj.ball_number.value = obj.innerHTML;
		HTML_AJAX.formSubmit('bingo_form','target_form');
		//obj.background = "images/bingo_mark.gif";
	},
	showCardMark : function() {
	if (card_num_obj)
		card_num_obj.style.backgroundColor = "#66CCFF";
	}
	//Bing Game Status functions -end

	
}


/*
function showGameStats() {
	var form_obj = document.bingo_form;
	form_obj.option.value = 'show_players';
	HTML_AJAX.formSubmit('bingo_form','players_div');
	showPopup('players_div','play_bingo_div');
}
function confirmToView(msg) {
	if (msg)
		alert(msg);
	else
		alert("Game has started.\r\nClick OK to view the game.");
	document.location = "index.php?page=playbingo";
}
function cancelGame() {
	var form_obj = document.bingo_form;
	form_obj.option.value = 'cancel_game';
	HTML_AJAX.formSubmit('bingo_form','target_form');
}
function bingo() {
	var form_obj = document.bingo_form;
	form_obj.action = 'check_bingo.php';
	HTML_AJAX.formSubmit('bingo_form','target_form');
	//var str = HTML_AJAX.grab("check_bingo.php");
	//alert(str);
}
function initiate_game(credits) {
	document.getElementById("credits_div").innerHTML = credits;
	monitorGame();
}
function start_play_bingo() {
			
		if (card_counter < 1) {
			alert("No card selected!");
			return false;
		}
		var form_obj = document.bingo_form;
		form_obj.option.value = 'join_game';
		form_obj.card_count.value = card_counter;
		HTML_AJAX.formSubmit('bingo_form','target_form');
}

function removeCard(card_slot) {
	document.getElementById("bingo_card" + card_slot).innerHTML = "&nbsp;";
	document.getElementById("bingo_card_button" + card_slot).disabled = true;
	card_counter--;
	if (card_slot == 1) {
		slot1 = false;
	} else if (card_slot == 2) {
		slot2 = false;
	} else if (card_slot == 3) {
		slot3 = false;
	} else if (card_slot == 4) {
		slot4 = false;
	}
}

function selectCard() {
	if (card_counter == 4) {
		alert("Maximum of 4 cards per screen.");
		return false;
	}
	card_counter++;
	if (slot1 == false) {
		card_slot = 1;
		slot1 = true;
	} else if (slot2== false) {
		card_slot = 2;
		slot2 = true;
	} else if (slot3 == false) {
		card_slot = 3;
		slot3 = true;
	} else if (slot4 == false) {
		card_slot = 4;
		slot4 = true;
	}
	var src_card = document.getElementById("bingo_card_selector");
	var target_card = document.getElementById("bingo_card" + card_slot);
	document.getElementById("bingo_card_button" + card_slot).disabled = false;
	var src_filename = document.getElementById("bingo_card_filename");
	var target_filename = document.getElementById("bingo_card" + card_slot + "_filename");
	target_filename.value = src_filename.value;
	target_card.innerHTML = src_card.innerHTML;
	src_card.innerHTML = "&nbsp;";
	getNextCard();	
}

function getNextCard() {
	var form_obj = document.bingo_form;
	form_obj.option.value = 'get_next_card';
	HTML_AJAX.formSubmit('bingo_form','bingo_card_selector');
}
function getPreviousCard() {
	var form_obj = document.bingo_form;
	form_obj.option.value = 'get_prev_card';
	HTML_AJAX.formSubmit('bingo_form','bingo_card_selector');
}

function confirmContinue() {
	
	//var answer = confirm("Waiting time is up, short of players!\r\nDo you want to cancel game?\r\nClick OK to cancel game or CANCEL to contiune waiting.");
	//if (answer) {
	//	cancelGame();
	//} else {
	//	if (game_handle != null)
	//		clearInterval(game_handle);
		//reset_game_time();
//	}
}

function startGame() {
	if (game_handle != null)
		clearInterval(game_handle);
	bingo_started = true;
	populateNumbers();
}
function markCardNumber(obj) {
	if (!bingo_started)
		return false;
	var form_obj = document.bingo_form;
	card_num_obj = obj;
	form_obj.option.value = 'mark_card';
	form_obj.ball_number.value = obj.innerHTML;
	HTML_AJAX.formSubmit('bingo_form','target_form');
	//obj.background = "images/bingo_mark.gif";
}
function checkGameStatus() {
	var form_obj = document.bingo_form;
	form_obj.option.value = 'check_game_status';
	HTML_AJAX.formSubmit('bingo_form','target_form');
	//alert('checking game');
}
function monitorGame() {
	 game_handle = setInterval('checkGameStatus()',1000);
}

function generate(base,val_id) {
	
	var val_obj = document.getElementById(val_id);
	var value = Math.floor((Math.random() * 100)%base) + 1;
	if (value < 10)
		val_obj.innerHTML = '0' + value;
	else
		val_obj.innerHTML = value;
	counter++;
	if (counter == 100) {
		var arr_len = arr_num.length; 
		for (var i=0; i < arr_len; i++) {
			if (arr_num[i] == value) {
				counter = 0;
				break;
			}
		}
		if (counter > 0) {
			arr_num[arr_len] = value;
			counter =0;
			stop();
		}
	}

}

function getNextBall() {
	
	var form_obj = document.bingo_form;
	form_obj.option.value = 'next_ball';
	form_obj.sequence_no.value = counter;
	HTML_AJAX.formSubmit('bingo_form','target_form');
	counter++;
	if (counter == 76) {
		stop();
		alert("Game Over");		
		reset_game();
	}
}

function move() {
	 handle = setInterval('getNextBall()',5000);
}

function stop() {
	
	if (handle != null)
		clearInterval(handle);
		
}

function reset_balls() {

	var form_obj = document.bingo_form;
	form_obj.option.value = 'reset_balls';
	HTML_AJAX.formSubmit('bingo_form','target_form');
	counter = 1;
	handle = null;
	//document.location.reload();
	//val_num = 0;
	//arr_num = new Array();
}

function reset_game_time() {
	var form_obj = document.bingo_form;
	form_obj.option.value = 'reset_game_time';
	HTML_AJAX.formSubmit('bingo_form','target_form');
	
}

function reset_game() {

	var form_obj = document.bingo_form;
	form_obj.option.value = 'reset_game';
	HTML_AJAX.formSubmit('bingo_form','target_form');
	
}

function initialize() {
	toggleButtons(true);
	document.getElementById("next_result").innerHTML = "&nbsp;&nbsp;";
}

function clear() {
	document.getElementById("next_result").innerHTML = "&nbsp;&nbsp;";
}

function setNextBallDisplay(next_ball,letter) {
	document.getElementById('next_ball').innerHTML= letter + " - " + next_ball;
	document.getElementById('tally_'+ next_ball).style.background="#00FF00";
	document.getElementById('ball_counter_span').innerHTML = counter-1;
}

function populateNumbers() {

	//showPopup('main_popup_div','btn_reset');
	var form_obj = document.bingo_form;
	form_obj.option.value = 'populate_all';
	HTML_AJAX.formSubmit('bingo_form','target_form');
	counter = 1;
	handle = null;
	//val_num = 0;
	//arr_num = new Array();
}
function plotBall(ball,letter) {
	document.getElementById('tally_'+ ball).style.background="#00FF00";
	//document.getElementById('tally_'+ ball).background = "images/red_title_marker.gif";
	document.getElementById('next_ball').innerHTML= letter + " - " + ball;
	counter++;
}

function setGameInfo(status,players,pot,remaining_time) {
	if (status == 1)
		status = "Waiting for other players.";
	if (status == 2)
		status = "Game has started.";
	if (status == 3)
		status = "Game has ended.";
	//document.getElementById('game_status').innerHTML = status;
	//document.getElementById('game_players').innerHTML = players;
	//document.getElementById('game_pot_prize').innerHTML =  pot;
	document.getElementById('game_remaining_time').innerHTML = remaining_time;	
	
}

function playbingonow() {
	
	window.open("start_playbingo.php","","menubar=0,resizable=0,width=1000,height=800");	
}

function showCardMark() {
	if (card_num_obj)
		card_num_obj.style.backgroundColor = "#66CCFF";
}

*/
