function displayFullSize(theImage, width, height)
{
	var myWin = window.open("","nCt", "top=50,left=50,width=" + (width+20) + ",height=" + (height+55) + ",status=no,toolbar=no,menubar=no,scrollbars=no");
	myWin.document.write("<html><head><title>" + theImage.alt + "</title></head>");
	myWin.document.write ("<body><center><img id='theImage' src='" + theImage.src + "' />");
	myWin.document.write ("<br/><a href='javascript:window.self.close()'>Close Window</a></center></body></html>");
}

function displayFullSizeFromPicker(theImage, width, height)
{
	var myWin = window.open("","nCt", "top=50,left=50,width=" + (width+20) + ",height=" + (height+55) + ",status=no,toolbar=no,menubar=no,scrollbars=no");
	myWin.document.write("<html><head><title>Image Preview ::" + theImage + "</title></head>");
	myWin.document.write ("<body><center><img id='theImage' src='" + theImage + "' />");
	myWin.document.write ("<br/><a href='javascript:window.self.close()'>Close Window</a></center></body></html>");
}  

function confirmDelete(elementType) {
	return confirm("Are you sure you want to delete this " + elementType + " ?");
}

function navigateTo(url) {
	if (url != "none") {
		window.document.location = url;
	}
}

function openHelpTopic(url) {
	window.open("helpPages/" + url, "helpWin", "top=50,left=50,width=450,height=600,status=no,toolbar=no,menubar=no,scrollbars=yes");
}

function changeHours(startOrEnd, upOrDown) {
	if (startOrEnd == "start") {
		var oHour = document.getElementById("EventEditor_newEventStartHour");
		var newTime = document.getElementById("EventEditor_newStartTime");
		var oMinutes = document.getElementById("EventEditor_newEventStartMinutes");
	} else {
		var oHour = document.getElementById("EventEditor_newEventEndHour");
		var newTime = document.getElementById("EventEditor_newEndTime");
		var oMinutes = document.getElementById("EventEditor_newEventEndMinutes");		
	}
	var hoursInt = parseInt(oHour.innerText);
	if (upOrDown == "up") {
		oHour.innerText = (hoursInt == 1) ? "12" : (hoursInt - 1);
	} else {
		oHour.innerText = (hoursInt == 12) ? "1" : (hoursInt + 1);
	}
	newTime.value = oHour.innerText + ":" + oMinutes.innerText;
}

function changeMinutes(startOrEnd, upOrDown) {
	if (startOrEnd == "start") {
		var oMinutes = document.getElementById("EventEditor_newEventStartMinutes");
		var newTime = document.getElementById("EventEditor_newStartTime");
		var oHour = document.getElementById("EventEditor_newEventStartHour");		
	} else {
		var oMinutes = document.getElementById("EventEditor_newEventEndMinutes");
		var newTime = document.getElementById("EventEditor_newEndTime");
		var oHour = document.getElementById("EventEditor_newEventEndHour");			
	}
	var minutesInt = parseInt(oMinutes.innerText);
	if (upOrDown == "up") {
		oMinutes.innerText = (minutesInt == 00) ? "45" : (minutesInt - 15);
		oMinutes.innerText = (oMinutes.innerText == "0") ? "00" : oMinutes.innerText;
	} else {
		oMinutes.innerText = (minutesInt == 45) ? "00" : (minutesInt + 15);	
	}
	newTime.value = oHour.innerText + ":" + oMinutes.innerText;
}

function revealChildInfoBoxes() {
	var numChildrenBox = document.getElementById("requestForm_tbNumChild");
	if (numChildrenBox != null) {
		var numChildren = 0;
		if (isNaN(numChildrenBox.value)) {
			alert("Please enter only whole numbers (1, 2, 3... etc) in the '# of Children' box.");
			numChildrenBox.value = "";
			numChildrenBox.focus();
		}
		else {
			numChildren = parseInt(numChildrenBox.value);
			if (numChildren > 8) {
				alert("We're sorry, but our form can only hold information for eight children.\n\n" +
						"Please enter information for your eight YOUNGEST children. Thank you!");
				numChildren = 8;
			}
			if (numChildren < 0) {
				alert("Negative numbers are not allowed in the '# of Children' box.\n\n" +
						"Please enter a positive number (1, 2, 3... etc).\n\nIf you have no children, enter 0.");
				numChildren = 0;
				numChildrenBox.value = "";
				numChildrenBox.focus();
			}
			for (var i = 0; i < numChildren; i++) {
				document.getElementById("requestForm_ChildTable" + (i+1)).style.display = "block";
			}
			for (var i = numChildren; i < 8; i++) {
				document.getElementById("requestForm_ChildTable" + (i+1)).style.display = "none";
			}
			if (numChildren > 0 && numChildren <= 8) {
				document.getElementById("requestForm_ChildTableLabel").style.display="block";
			}
			else {
				document.getElementById("requestForm_ChildTableLabel").style.display="none";
			}
		}

	}
}

// the link, text block, or image currently under consideration for editing
var currentObject;

function clickTopImage(topImage) {
	if (currentObject != null) {
		currentObject.style.borderColor = "white";
	}
	var position = topImage.id.substring(1,topImage.id.length);
	document.getElementById("ImagePosition").value = position;
	if (position == "LogoCell_Top") {
		alert("You have selected an image that appears on EVERY web page.\n\nBe aware that modifying this image will change it on EVERY web page.");
	}
	currentObject = topImage;
	currentObject.style.borderColor = "black";
	__doPostBack('LaunchImagePickerButton', '');
}

function clickTextBlock(textBlock) {
	if (currentObject != null) {
		currentObject.style.borderColor = "white";
	}
	document.getElementById("TextPosition").value = textBlock.id;
	document.getElementById("CurrentValue").value = textBlock.innerHTML;
	if (textBlock.id == "Banner_Bottom") {
		alert("You have selected a block of text that appears on EVERY web page.\n\nBe aware that modifying this text will change it on EVERY web page.");
	}
	currentObject = textBlock;
	currentObject.style.borderColor = "black";
	__doPostBack('LaunchTextEditorButton', '');
}

function clickLink(linkBlock) {
	if (currentObject != null) {
		currentObject.style.borderColor = "white";
	}
	currentObject = linkBlock;
	currentObject.style.borderColor = "black";
	__doPostBack('LaunchLinkPickerButton', '');
}

function showHelpTip(e, sHtml, bHideSelects) {

	// find anchor element
	var el = e.target || e.srcElement;
	while (el.tagName != "A")
		el = el.parentNode;
	
	// is there already a tooltip? If so, remove it
	if (el._helpTip) {
		helpTipHandler.hideHelpTip(el);
	}

	helpTipHandler.hideSelects = Boolean(bHideSelects);

	// create element and insert last into the body
	helpTipHandler.createHelpTip(el, sHtml);
	
	// position tooltip
	helpTipHandler.positionToolTip(e);

	// add a listener to the blur event.
	// When blurred remove tooltip and restore anchor
	el.onblur = helpTipHandler.anchorBlur;
	el.onkeydown = helpTipHandler.anchorKeyDown;
}

var helpTipHandler = {
	hideSelects:	false,
	
	helpTip:		null,
	
	showSelects:	function (bVisible) {
		if (!this.hideSelects) return;
		// only IE actually do something in here
		var selects = [];
		if (document.all)
			selects = document.all.tags("SELECT");
		var l = selects.length;
		for	(var i = 0; i < l; i++)
			selects[i].runtimeStyle.visibility = bVisible ? "" : "hidden";	
	},
	
	create:	function () {
		var d = document.createElement("DIV");
		d.className = "help-tooltip";
		d.onmousedown = this.helpTipMouseDown;
		d.onmouseup = this.helpTipMouseUp;
		document.body.appendChild(d);		
		this.helpTip = d;
	},
	
	createHelpTip:	function (el, sHtml) {
		if (this.helpTip == null) {
			this.create();
		}

		var d = this.helpTip;
		d.innerHTML = sHtml;
		d._boundAnchor = el;
		el._helpTip = d;
		return d;
	},
	
	// Allow clicks on A elements inside tooltip
	helpTipMouseDown:	function (e) {
		var d = this;
		var el = d._boundAnchor;
		if (!e) e = event;
		var t = e.target || e.srcElement;
		while (t.tagName != "A" && t != d)
			t = t.parentNode;
		if (t == d) return;
		
		el._onblur = el.onblur;
		el.onblur = null;
	},
	
	helpTipMouseUp:	function () {
		var d = this;
		var el = d._boundAnchor;
		el.onblur = el._onblur;
		el._onblur = null;
		el.focus();
	},	
	
	anchorBlur:	function (e) {
		var el = this;
		helpTipHandler.hideHelpTip(el);
	},
	
	anchorKeyDown:	function (e) {
		if (!e) e = window.event
		if (e.keyCode == 27) {	// ESC
			helpTipHandler.hideHelpTip(this);
		}
	},
	
	removeHelpTip:	function (d) {
		d._boundAnchor = null;
		d.style.filter = "none";
		d.innerHTML = "";
		d.onmousedown = null;
		d.onmouseup = null;
		d.parentNode.removeChild(d);
		//d.style.display = "none";
	},
	
	hideHelpTip:	function (el) {
		var d = el._helpTip;
		/*	Mozilla (1.2+) starts a selection session when moved
			and this destroys the mouse events until reloaded
		d.style.top = -el.offsetHeight - 100 + "px";
		*/		
		
		d.style.visibility = "hidden";
		//d._boundAnchor = null;

		el.onblur = null;
		el._onblur = null;
		el._helpTip = null;
		el.onkeydown = null;
		
		this.showSelects(true);
	},
	
	positionToolTip:	function (e) {
		this.showSelects(false);		
		var scroll = this.getScroll();
		var d = this.helpTip;
		
		// width
		if (d.offsetWidth >= scroll.width)
			d.style.width = scroll.width - 10 + "px";
		else
			d.style.width = "";
		
		// left
		if (e.clientX > scroll.width - d.offsetWidth)
			d.style.left = scroll.width - d.offsetWidth + scroll.left + "px";
		else
			d.style.left = e.clientX - 2 + scroll.left + "px";
		
		// top
		if (e.clientY + d.offsetHeight + 18 < scroll.height)
			d.style.top = e.clientY + 18 + scroll.top + "px";
		else if (e.clientY - d.offsetHeight > 0)
			d.style.top = e.clientY + scroll.top - d.offsetHeight + "px";
		else
			d.style.top = scroll.top + 5 + "px";
			
		d.style.visibility = "visible";
	},
	
	// returns the scroll left and top for the browser viewport.
	getScroll:	function () {
		if (document.all && typeof document.body.scrollTop != "undefined") {	// IE model
			var ieBox = document.compatMode != "CSS1Compat";
			var cont = ieBox ? document.body : document.documentElement;
			return {
				left:	cont.scrollLeft,
				top:	cont.scrollTop,
				width:	cont.clientWidth,
				height:	cont.clientHeight
			};
		}
		else {
			return {
				left:	window.pageXOffset,
				top:	window.pageYOffset,
				width:	window.innerWidth,
				height:	window.innerHeight
			};
		}
		
	}

};