/**
 * Library Code for WEB150
 * @version 1.0
 */
 
/**
 * attaches an event handler function on an object
 * @param object obj The object or element reference 
 * (e.g. the return value of a document.getElementById('...') call)
 * @param string type The event name to listen for
 * (e.g. 'load', 'click', 'mousedown', etc)
 * @param function fn The function to run when the event fires
 */
function addEvent( obj, type, fn ) {
	if ( obj.attachEvent ) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	} else
		obj.addEventListener( type, fn, false );
}
/**
 * removes an event handler function from an object
 * @param object obj The object or element reference
 * @param string type The event name
 * @param function fn The function to remove
 */
function removeEvent( obj, type, fn ) {
	if ( obj.detachEvent ) {
	    obj.detachEvent( 'on'+type, obj[type+fn] );
	    obj[type+fn] = null;
	} else
	    obj.removeEventListener( type, fn, false );
    }

/**
 * validEmail checks for a single valid email
 *
 * supported RFC valid email addresses:
 * a@a.com
 * A_B@A.co.uk
 * abc.123@a.net
 * 12+34-5+1=42@a.org
 * adam&lena@a.co.uk
 * root!@a_b.com
 * _______@a-b.la
 * %&=+.$#!-@a.com
 *
 * Current known unsupported (but are RFC valid):
 * abc+mailbox/department=shipping@example.com
 *  !#$%&'*+-/=?^_`.{|}~@example.com (all of these characters are RFC compliant)
 * "abc@def"@example.com (anything goes inside quotation marks)
 * "Fred \"quota\" Bloggs"@example.com (however, quotes need escaping)
 *
 * @param string email The supposed email address to validate
 * @return bool valid
 * @author Adam Eivy
 * @version 2.0
 * @codesnippitID bcd71ab9-dc05-45af-9855-abb57c0cf0ab
 */
function validEmail(email){
   var re = /^[\w.%&=+$#!-]+@[\w.-]+\.[a-zA-Z]{2,4}$/;
   return re.test(email);
};

/**
 * sets the background color on an 
 * element to be yellow (highlighted)
 */
function highlightField(el){
	el.style.backgroundColor = 'yellow';
}

/**
 * sets the background color on an 
 * element to be white (unhighlighted)
 */
function unHighlightField(el){
	el.style.backgroundColor = 'white';
}

function cancelEvent(e){
	e.cancelBubble = false;
	e.returnValue = false;
	e.preventDefault();
	return false;
}

