function openWindow(form, popName, widthVal, heightVal, resizeVal){
	var xx = null;
	// convert heightVal to integer to allow for addition of 50px for TopPosition
	heightVal = parseInt(heightVal);
	LeftPosition = (screen.width) ? (screen.width-widthVal)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-(heightVal+50))/2 : 0;
	if (LeftPosition < 0) {	LeftPosition = 0;}
	if (TopPosition < 0) {	TopPosition = 0;}
	windowprops = "left="+LeftPosition+",top="+TopPosition+",width="+widthVal+",height="+heightVal+",location=no,scrollbars=no,menubars=no,toolbars=no,resizable="+resizeVal+",fullscreen=no";
	xx = window.open("", popName, windowprops);
	form.target=popName;
	form.submit();
	xx.focus();
	return false;
}

var numloops = 0;
var bingoWindowMap = new Object();

function padout(number){
	return (number < 10) ? '0' + number : number;
}

function disableDeposit() {
	if(document.getElementById("depositWindowButton").name == "disabled") {
		document.getElementById("depositWindowButton").disabled = "disabled";
		document.getElementById("duplicateclick").style.visibility = 'visible';
	}
	document.getElementById("depositWindowButton").name = "disabled";
}

function setPromo(theform) {

	document.getElementById("promoCode").value = document.getElementById("promo").value;
}

function disableDays() {
	
	if(document.selfExclusion.exclusionStatus.value == "G") {
		document.selfExclusion.numDays.disabled = "disabled";
	} else if(document.selfExclusion.exclusionStatus.value == "F"){
		document.selfExclusion.numDays.disabled = "";
	}
}

function eventsContainers(thebox) {
	document.getElementById("detailtab1").className = "hide";
	document.getElementById("detailtab2").className = "hide";
	document.getElementById("detailtab3").className = "hide";
	document.getElementById("detailtab4").className = "hide";
document.getElementById("detailtab5").className = "hide";
document.getElementById("detailtab6").className = "hide";
document.getElementById("detailtab7").className = "hide";
document.getElementById("detailtab8").className = "hide";
document.getElementById("detailtab9").className = "hide";
document.getElementById("detailtab10").className = "hide";
document.getElementById("detailtab11").className = "hide";
document.getElementById("detailtab12").className = "hide";
document.getElementById("detailtab13").className = "hide";
document.getElementById("detailtab14").className = "hide";
document.getElementById("detailtab15").className = "hide";
document.getElementById("detailtab16").className = "hide";
	
	document.getElementById(thebox).className = "show";

}

function updateClocks(){
	numloops = numloops + 1;
	var d = new Date();
	var tmp;
	var hasStarted = 0;
	var rtmp;
	var refreshInSec = 30; 
	var shouldRefresh = 0;
	
	$("#bingorooms form.bingoForm").each(function(){
		var currentFormId = $(this).attr("id");
		var currentTime = $(this).find("input[name='STARTTIME']");
		var currentSpan = $(this).find("span[id='clock-" + currentFormId + "']");
		tmp = currentTime.val() - 0;
		rtmp = -1;

			d = new Date(tmp);
			tmp = tmp - 1000;
			if (tmp < 0) {
				//tmp = 0;
			} else if (tmp >= (refreshInSec * 1000) && tmp <= (331 * 1000) ) {
				rtmp = Math.floor(tmp / 1000);
				rtmp %= refreshInSec;
				if (rtmp == 0) {
					shouldRefresh = 1;
				}
			}

			if (tmp <= 0) {
				currentSpan.text("STARTED");
				currentTime.val(tmp);
				if (tmp < -3000) {
				   hasStarted = 1;
				}
			} else {
				currentSpan.text(padout(d.getUTCHours()) + ':' + padout(d.getUTCMinutes()) + ':' + padout(d.getUTCSeconds()) );
				currentTime.val(tmp);
			}
	});

	if (hasStarted == 1) {
		if ($("#bingorooms") != null) {
			$("#bingorooms").html('');
			$("#bingorooms").load("/games/reloadGames");
		}
		hasStarted = 0;
	} else {
		if (shouldRefresh != 0) {
			refreshGameInfo(refreshInSec );
		}
	}
}

function refreshGameInfo(refreshInSec ){
	var $bingoForm;
	var currentAjax = $('#ajax-loader');
	var d = new Date();
	var tmpText = '';
	var tmp;
	var tmpObj;
	var rtmp;

	$.get( '/games/refreshGames', {time: d.getTime()}, function(data) {
	   var $response = $('<div />').html(data);
	   $response.find("form.bingoForm").each(function(){
		var bingoId = $(this).attr("id").substring(5);
		var bingoNPlayers = $(this).find("input[name='nplayers']");
		var bingoPrize = $(this).find("input[name='prize']");
		var bingoPJP = $(this).find("input[name='pjp']");
		var newStartTime = $(this).find("input[name='STARTTIME']");
		tmpText = tmpText + '<br/>' + bingoId + '=' + bingoPrize.val();
		tmp = newStartTime.val() - 0;
		if (tmp > 0) {
			if (tmp < 0) tmp = 0;
		}
		rtmp = Math.floor(tmp / 1000);
		rtmp %= refreshInSec;
		if (rtmp == 0) {
			tmp -= 1000;
		}

		$bingoForm = null;
		$bingoForm = $("#" + bingoId);
		if ($bingoForm != null && tmp > 0) {
		   	tmpObj = $bingoForm.find("input[name='STARTTIME']");
			if (tmpObj != null) {
				tmpObj.val(tmp);
			}

			d = new Date(tmp);
			tmpObj = $bingoForm.find("span[id='clock-" + bingoId+ "']");
			if (tmpObj != null) {
				tmpObj.text(padout(d.getUTCHours()) + ':' + padout(d.getUTCMinutes()) + ':' + padout(d.getUTCSeconds()));
			}

			tmpObj = $("#nplayers-" + bingoId);
			if (tmpObj != null) {
				tmpObj.text( bingoNPlayers.val());
			}

			tmpObj = $("#prize-" + bingoId);
			if (tmpObj != null) {
				tmpObj.text( bingoPrize.val());
			}

			tmpObj = $("#pjp-" + bingoId);
			if (tmpObj != null) {
				tmpObj.text( bingoPJP.val());
			}
		}
	   });
	}, 'html' );
}

function popBingo(form, popName) {
	allowResize = "no";
	allowResize = "yes";
			// Start at top left
			LeftPosition = 0;
			TopPosition = 0;
			// Go almost full-screen for Flash Bingo
			myHeight = (screen.height) ? (screen.height-145) : 0;
			myWidth = myHeight * 4 / 3;

	
	// Set the left and top window position to zero if logic above has produced a value less than zero
	if (LeftPosition < 0) {	LeftPosition = 0;}
	if (TopPosition < 0) {	TopPosition = 0;}
	// Configure other window properties
	windowprops = "left="+LeftPosition+",top="+TopPosition+",width="+myWidth+",height="+myHeight+",location=0,scrollbars=0,menubar=0,toolbar=0,resizable=" + allowResize;
	// Open window as object
	// IS1315: ensure this window is closed to prevent error message from appearing 
	if (bingoWindowMap[popName] != undefined) bingoWindowMap[popName].close();

	bingoWindowMap[popName] = window.open("", popName, windowprops);
	// Set form target to bingo window name
	form.target = popName;
	// Submit form
	form.submit();
	// Focus on bingo window by object reference
	bingoWindowMap[popName].focus();
	return true;
}


jQuery.fn.uncheck = function() {
   return this.each(function() {
     this.checked = false;
   });
};

jQuery.fn.check = function() {
   return this.each(function() {
     this.checked = true;
   });
};

jQuery.fn.TreeViewCheckboxesOff = function() {
	$(this).each(function() {
		$(this).parent().find(":checkbox").uncheck();
		$(this).removeClass("checkbox-checked").addClass("checkbox-unchecked").removeClass("checkbox-partial");
	});
};

jQuery.fn.TreeViewCheckboxesOn = function() {
	$(this).each(function() {
		$(this).parent().find(":checkbox").check();
		$(this).addClass("checkbox-checked").removeClass("checkbox-unchecked").removeClass("checkbox-partial");
	});
};

jQuery.fn.TreeViewCheckboxesToggle = function(){
	var check = $(":checkbox[id='"+this.attr("for")+"']")[0];
	var wasChecked = check.checked;

	var parentListItem = this.parent();
	var parentNodes = parentListItem.parents().filter(".expandable, .collapsable");
	var hasChildren = $(parentListItem).hasClass("expandable") || $(parentListItem).hasClass("collapsable");

	if (hasChildren) {
		// An expandable node was clicked.  We have to either check/uncheck all children below.
		var childItems = $(parentListItem).find("li label");
		if (wasChecked) {
			$(childItems).TreeViewCheckboxesOff();
			this.addClass("checkbox-checked").removeClass("checkbox-unchecked").removeClass("checkbox-partial");
		} else {
			$(childItems).TreeViewCheckboxesOn();
			this.removeClass("checkbox-checked").addClass("checkbox-unchecked").removeClass("checkbox-partial");
		}
	}

	// Now, traverse up the tree to update any higher-level checkboxes
	if (parentNodes.length > 0) {
		$(parentNodes).each(function(){
			var label = $(this).children("label");
			var allChildrenSize = $(this).find("li :checkbox").length;
			var checkedChildrenSize = $(this).find("li :checkbox:checked").length;

			if (wasChecked) {
				checkedChildrenSize -= 1;
			} else {
				checkedChildrenSize += 1;
			}

			var theCheckBox = $(":checkbox[id='"+$(label).attr("for")+"']"); 
			if (allChildrenSize != checkedChildrenSize && checkedChildrenSize == 0) {
				$(label).removeClass("checkbox-partial").removeClass("checkbox-checked").addClass("checkbox-unchecked");
				theCheckBox.uncheck();
			} else if (allChildrenSize != checkedChildrenSize) {
				$(label).addClass("checkbox-partial").removeClass("checkbox-checked").removeClass("checkbox-unchecked");
				theCheckBox.uncheck();
			} else {
				$(label).addClass("checkbox-checked").removeClass("checkbox-unchecked").removeClass("checkbox-partial");
				theCheckBox.check();
			}
		});
	}

	// Click the hidden input box for IE only.  Firefox will cascade the click down.
	if ($.browser.msie) {
		check.click();
	}

	this.toggleClass("checkbox-checked").toggleClass("checkbox-unchecked");
}

jQuery.fn.TreeViewCheckboxes = function(){
	$(":checkbox", this)
	// Hide native checkboxes
	.hide()
	// Find related labels and add the styles
	.each(function(){
		var check = this;
		var jlabel = $("label[for='"+$(check).attr("id")+"']");
		var disabled = $(check).attr("disabled");

		// Initial state check
		if (check.checked) {
			if (!check.disabled) {
				jlabel.addClass("checkbox-checked");
			} else {
				jlabel.addClass("checkbox-checked-disabled");
			}
		} else {
			if (!check.disabled) {
				jlabel.addClass("checkbox-unchecked");
			} else {
				jlabel.addClass("checkbox-unchecked-disabled");
			}
		}

		jlabel.hover(
			function() { $(this).addClass("over"); },
			function() { $(this).removeClass("over"); }
		)

		// Label click state
		jlabel.click(function(){
			var check = $(":checkbox[id='"+$(this).attr("for")+"']")[0];
			if ($(check).attr("disabled") != true) {
				$(this).TreeViewCheckboxesToggle();
			}
		});
	});
	var treeNodes = $(this).find("li:first").markInputs();
}

jQuery.fn.markInputs = function() {
	var treeNodes = $(this).find("li.expandable, li.collapsable").andSelf();
	treeNodes.each(function() {
		$(this).markInput();
	});
};

jQuery.fn.markInput = function() {
	var label = $(this).children("label");
	var check = $(this).children(":checkbox");
	var allChildrenSize = $(this).find("li :checkbox").length;
	var checkedChildrenSize = $(this).find("li :checkbox:checked").length;
	
	if (allChildrenSize != checkedChildrenSize && checkedChildrenSize == 0) {
		if ($(check).attr("disabled") != true) {
			$(label).removeClass("checkbox-partial").removeClass("checkbox-checked").addClass("checkbox-unchecked");
		} else {
			$(label).removeClass("checkbox-partial-disabled").removeClass("checkbox-checked-disabled").addClass("checkbox-unchecked-disabled");
		}
		$(check).uncheck();
	} else if (allChildrenSize != checkedChildrenSize) {
		if ($(check).attr("disabled") != true) {
			$(label).addClass("checkbox-partial").removeClass("checkbox-checked").removeClass("checkbox-unchecked");
		} else {
			$(label).addClass("checkbox-partial-disabled").removeClass("checkbox-checked-disabled").removeClass("checkbox-unchecked-disabled");
		}
		$(check).uncheck();
	} else {
		if ($(check).attr("disabled") != true) {
			$(label).addClass("checkbox-checked").removeClass("checkbox-unchecked").removeClass("checkbox-partial");
		} else {
			$(label).addClass("checkbox-checked-disabled").removeClass("checkbox-unchecked-disabled").removeClass("checkbox-partial-disabled");
		}
		$(check).check();
	}
};