/* Form Focus
 * 
 * When the page loads, the first form on the page,
 * (if there is one) should receive the focus.
 */
Event.observe(window, 'load', function() {
	var form = $$("form").first();
	if (form)
	{
		var elem = form.findFirstElement();
		if (elem && !elem.hasClassName("no-focus"))
		{
			elem.focus();
		}
	}
});

/* Date Field (onfocus, onblur) 
 *
 * When the page loads, all date fields should be set
 * to the correct onfocus/onblur behavior regarding mm/dd/yy.
 */
Event.observe(window, 'load', function() {
	$$("input.ffDate").each(function(ffDate) {
		// onload, make "mm/dd/yyyy" if ""
		if ($F(ffDate) == "")
		{
			ffDate.value = "mm/dd/yyyy";
		}
	
		// focus, make "" if "mm/dd/yyyy"
		Event.observe(ffDate, "focus", function(e) {
			var el = Event.element(e);
			if (el.value == "mm/dd/yyyy")
			{
				el.value = "";
			}
		});
		
		// blur, make "mm/dd/yyyy" if ""
		Event.observe(ffDate, "blur", function(e) {
			var el = Event.element(e);
			if (el.value == "")
			{
				el.value = "mm/dd/yyyy";
			}
		});
	});
});

/* Date Field (onsubmit) 
 *
 * When the page loads, all forms should have an
 * onsubmit that is going to look for date fields, and
 * if it finds them, clear out the "mm/dd/yyyy" values
 * that they have.
 */
Event.observe(window, 'load', function() {
	$$("form").each(function(form) {
		Event.observe(form, "submit", function() {
			$$("input.ffDate").each(function(ffDate) {
				if (ffDate.value == "mm/dd/yyyy")
				{
					ffDate.value = "";
				}
			});
		});
	});
});

/* Simple Pop Ups
 * 
 * Everyone hates pop-ups, but at least this makes it easy to create them, and has
 * them degrade gracefully back to normal links for people who don't have javascript.
 */
function popUpObserver(event) 
{
	var element = Event.element(event);
	var link = element.nodeName == "A" ? element : element.up("a.popup");
	
	var options = "location=yes,menubar=yes,status=yes,toolbar=yes,resizable=yes,scrollbars=yes";
	if (link.hasClassName("no-chrome"))
	{
		options = "location=no,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes";
	}
	
	window.open(link.readAttribute("href"), "", options);
	Event.stop(event); // don't follow the link as normal...
}

Event.observe(window, 'load', function() {
	$$("a.popup").each(function(link) {
		Event.observe(link, "click", popUpObserver);
	});
});

/* Autocomplete Off
 *
 * Sometimes it is necessary to turn off form autocompletion.
 */
Event.observe(window, 'load', function() {
	$$("input.noAutoComplete").each(function(element) { 	// TODOJS selector could be made more specific, and therefore sped up
		element.setAttribute("autocomplete", "off");
	});
});

/* getInnerDimensions
 *
 * This function attempts to dependably return the current size of the screen
 * real estate taken up by the viewport of the browser window (which is to say
 * the actual amount of the screen that shows the page at this point).
 * 
 * This is deprecated by Prototype 1.6, which has its own version.
 */
function getInnerDimensions() 
{	// from quirksmode
	var x, y;
	
	if (self.innerHeight)
	{ // all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{ // Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body)
	{ // other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	
	return {"width" : x, "height" : y};
}