var bingoTimer = "";
var reloadWindow = false;

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;
}

function depositWindow(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=yes,menubars=no,toolbars=no,resizable="+resizeVal+",fullscreen=no";
	xx = window.open("", popName, windowprops);
	form.target=popName;
	form.submit();
	xx.focus();
	return false;
}

function launchWagerworks(url, popName, widthVal, heightVal){
	windowprops = ",width="+widthVal+",height="+heightVal+",location=no,scrollbars=no,menubars=no,toolbars=no,fullscreen=no";
	xx = window.open(url, "popWin" + popName, windowprops);
	xx.focus();
	return false;
}

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

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

function setPromoCode(theForm) {
	document.getElementById("promoCode").value = document.getElementById("promoCodeVisible").value;
	theForm.submit();
}

function updateClocks(){
	numloops = numloops + 1;
	var d = new Date();
	var tmp;
	var reloadWindow = false; 
	if (reloadWindow == false) { 
		$("form.bingoForm").each(function(){
			if (!reloadWindow) {
				var currentFormId = $(this).attr("id");
				var currentTime = $(this).find("input[name='STARTTIME']");
				var currentSpan = $("span[id='clock-" + currentFormId + "']");
//				alert(currentFormId);
				tmp = currentTime.val() - 0;
				if (tmp > 0) {
					d = new Date(tmp);
					tmp = tmp - 1000;
					if (tmp < 0) tmp = 0;
					currentSpan.text(padout(d.getUTCHours()) + ':' + padout(d.getUTCMinutes()) + ':' + padout(d.getUTCSeconds()));
					currentTime.val(tmp);
				} else if (tmp == 0) {
					currentSpan.text("STARTED");
					currentTime.val(0);
					window.clearInterval(bingoTimer);
					//reloadWindow = true;
				}
			}
		});
	};
	if (reloadWindow == true) {
		//window.location.reload(true);
	}
}

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();
	}
};