/*
//	Adam's Common JavaScript Library :: Event Attachment-related Functions
//	Copyright (c) 2010-2011 - Adam Rehn
*/

//Attach an event event handler to an element's event
function attach(elem_name, event, func)
{
	//Get a handle on the desired element
	var elem;
	if (typeof(elem_name) == 'object')
		elem = elem_name;
	else
		elem = document.getElementById(elem_name); 
	
	//If we got the handle successfully, try to attach the event
	if (typeof(elem) != 'undefined' && elem != null)
	{
		if (elem.addEventListener)
		{
			//Version for sane browsers
			elem.addEventListener(event, func, false);
		}
		else
		{
			//Version for IE
			//elem.attachEvent('on' + event, func); //This wasn't working
			elem['on' + event] = func;
		}
	}
}

//Function for focussing a form field to be emptied
function focusField(elem, value)
{
	//If the element doesn't exist, don't bother trying to do anything
	if (typeof(elem) == 'undefined' || elem == null || !elem.style)
		return;
	
	//Empty the field if it contained the default value
	if (elem.value == value)
	{
		elem.value = "";
	}
	
	//Set the style to focussed
	elem.style.color = '#000';
}

//Function for blurring a form field to be emptied
function blurField(elem, value)
{
	//If the element doesn't exist, don't bother trying to do anything
	if (typeof(elem) == 'undefined' || elem == null || !elem.style)
		return;
	
	//If the field is empty, fill it with its default value and set the style to blurred
	if (elem.value == "")
	{
		//Set the style to blurred
		elem.style.color = '#999';
		
		//Restore the default value
		elem.value = value;
	}
}

