﻿// Functions to aid handling events

// General function for adding an event listener
function addEvent(obj, evType, fn, useCapture) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent('on' + evType, fn);
		return r;
	} else {
		//alert(evType + ' handler could not be attached');
	}
}

// Specific function for this particular browser
function addKeyDownEvent(obj, fn) {
	addEvent(obj, 'keydown', fn, false);
}
function addKeyPressEvent(obj, fn) {
	addEvent(obj, 'keypress', fn, false);
}

// Returns the source object that fired the event
function getEventSource(evt) {
	return (window.event) ? evt.srcElement : evt.target;
}

// Returns keycode for a key event
function getEventKeyCode(evt) {
	return (evt) ? ((window.event) ? evt.keyCode : evt.which) : null;
}

// Appends a function call to the window.onload event
function addLoadEvent(fn) {
	var oldOnload = window.onload;
	if (typeof oldOnload == "function") {
		window.onload = function() {
			if (oldOnload) oldOnload();
			fn();
		};
	} else {
		window.onload = fn;
	} 
}
