var inputhighlighter_last_focus_element = false;
var inputhighlighter_last_focus_element_value = false;

function inputhighlighter_on(e)
{
	var current_obj = false;

	if(e && e.srcElement)
	{
		current_obj = e.srcElement;
	}
	else if(this && this.style)
	{
		current_obj = this;
	}

	if( current_obj!=false && getStyle(current_obj, "background-color") )
	{
		if(window.inputhighlighter_last_focus_element!=false && inputhighlighter_last_focus_element_value!= false)
		{
			window.inputhighlighter_last_focus_element.style.backgroundColor=window.inputhighlighter_last_focus_element_value;
		}

		window.inputhighlighter_last_focus_element = current_obj;


		window.inputhighlighter_last_focus_element_value = getStyle(current_obj, "background-color");

		current_obj.style.backgroundColor = "#e5ddb7";
	}

	inputhighlighter_restore_handlers(current_obj, "focus");

	return true;
}
function inputhighlighter_off(e)
{
	var current_obj = false;

	if(e && e.srcElement)
	{
		current_obj = e.srcElement;
	}
	else if(this && this.style)
	{
		current_obj = this;
	}

	if( current_obj!=false )
	{
		if(window.inputhighlighter_last_focus_element!=false && inputhighlighter_last_focus_element_value!= false)
		{
			window.inputhighlighter_last_focus_element.style.backgroundColor=window.inputhighlighter_last_focus_element_value;
			window.inputhighlighter_last_focus_element = false;
			window.inputhighlighter_last_focus_element_value = false;
		}
	}

	inputhighlighter_restore_handlers(current_obj, "blur");

	return true;
}
function inputhighlighter_add_listeners_to_all_clickelements()
{
	if(document.getElementsByTagName)
	{
		for(input=0, l=document.getElementsByTagName("input").length; input < l; input++)
		{
			if(
				document.getElementsByTagName("input")[input].getAttribute 
				&& document.getElementsByTagName("input")[input].getAttribute("type")!="button"
				&& document.getElementsByTagName("input")[input].getAttribute("type")!="submit"
			)
			{
				inputhighlighter_preserve_handlers(document.getElementsByTagName("input")[input], "focus");
				addEvent(document.getElementsByTagName("input")[input], "focus", inputhighlighter_on);

				inputhighlighter_preserve_handlers(document.getElementsByTagName("input")[input], "blur");
				addEvent(document.getElementsByTagName("input")[input], "blur", inputhighlighter_off);
			}
		}
		for(input=0, l=document.getElementsByTagName("select").length; input < l; input++)
		{
			if(!document.all)
			{
				// ie will close select after style manipulation so user has to click twice
				inputhighlighter_preserve_handlers(document.getElementsByTagName("select")[input], "focus");
				addEvent(document.getElementsByTagName("select")[input], "focus", inputhighlighter_on);
				
				inputhighlighter_preserve_handlers(document.getElementsByTagName("select")[input], "blur");
				addEvent(document.getElementsByTagName("select")[input], "blur", inputhighlighter_off);
			}
		}
		for(input in document.getElementsByTagName("textarea"))
		{
			inputhighlighter_preserve_handlers(document.getElementsByTagName("textarea")[input], "focus");
			addEvent(document.getElementsByTagName("textarea")[input], "focus", inputhighlighter_on);
			
			inputhighlighter_preserve_handlers(document.getElementsByTagName("textarea")[input], "blur");
			addEvent(document.getElementsByTagName("textarea")[input], "blur", inputhighlighter_off);
		}
	}
}

function inputhighlighter_preserve_handlers(obj, name_of_event)
{
	if(obj && obj.getAttribute && obj.setAttribute)
	{
		if(obj.getAttribute("on"+name_of_event) != null && obj.getAttribute("on"+name_of_event) != "")
		{
			obj.setAttribute("inputhighlighter_"+name_of_event, obj.getAttribute("on"+name_of_event));
			obj.removeAttribute("on"+name_of_event, 0);
		}
	}
}

function inputhighlighter_restore_handlers(obj, name_of_event)
{
//	return true;
	if(obj && obj.getAttribute && obj.setAttribute)
	{
		try
		{
			// ff stores onclick as string. let's generate an executable function and run it withinin current_object context to preserve .this references
			if(typeof obj.getAttribute("inputhighlighter_"+name_of_event) == "string" && obj.getAttribute("inputhighlighter_"+name_of_event) != "")
			{
				eval('obj.tmp=function(){'+obj.getAttribute("inputhighlighter_"+name_of_event)+';}');
				obj.tmp();
			}
			// ie stores onclick as function. let's add and run it withinin current_object context to preserve .this references
			else if(typeof obj.getAttribute("inputhighlighter_"+name_of_event) == "function")
			{
				obj.getAttribute("inputhighlighter_"+name_of_event)();
			}
		}
		catch(e)
		{
//			alert(e);
		}
	}
}

add_onload_action("inputhighlighter_add_listeners_to_all_clickelements()");