/* common js functions */

/* quick getElement reference */
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

/* addEvent: simplified event attachment */
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else 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] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
	
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

/* window 'load' attachment */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function findTarget(e) {
  var el;
  if(window.event && window.event.srcElement) {
    el = window.event.srcElement;
  } else if (e && e.target){
    el = e.target;
  }
  if(!el) el = null;
  return el;
}
  
/* basic DOM traversal functions */
if(typeof dom=='undefined'){
  dom = {
    next: function(e) {
       do {
       	e = e.nextSibling;
       } while(e && e.nodeType!=1);
       return e;
    },
    
    prev: function(e) {
    	do {
    		e = e.previousSibling;
    	} while(e && e.nodeType!=1);
    	return e;
    },
    
    parent: function(e) {
    	do {
    		e = e.parentNode;
    	} while(e && e.nodeType!=1);
    	return e;
    },
    
    ascend: function(e, target, id) {
    	do {
    	  e = e.parentNode;
    	} while(e.nodeName.toLowerCase()!=target.toLowerCase() && e.nodeName.toLowerCase() != 'html');
    	return (e.nodeName.toLowerCase() == 'html') ? null : e;
    },
    
    first: function(e) {
    	e = e.firstChild;
    	return (e && e.nodeType!=1) ? next(e) : e;
    },
    
    last: function(e) {
    	e = e.lastChild;
    	return (e && e.nodeType!=1) ? prev(e) : e;
    },
  
    insertAfter: function(newElement, targetElement) {
    	//target is what you want it to go after. Look for this elements parent.
    	var parent = targetElement.parentNode;
    	
    	//if the parents lastchild is the targetElement...
    	if(parent.lastchild == targetElement) {
    		//add the newElement after the target element.
    		parent.appendChild(newElement);
    	} else {
    		// else the target has siblings, insert the new element between the target and it's next sibling.
    		parent.insertBefore(newElement, targetElement.nextSibling);
    	}
    }
  }
}

function getAbsPos(obj) {
	var x = 0;
	var y = 0;
	while(obj.offsetParent)
	{
		y += obj.offsetTop;
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return {X:x,Y:y}
}

/* POP-UP WINDOWS */
// generic multi use pop-up with pre-programmed 'type'
var newWin = null;
function popUp(strURL, strType, strHeight, strWidth) {
	if (newWin != null && !newWin.closed) newWin.close();
	var strOptions="";
	if (strType=="console") strOptions="resizable";
	//if (strType=="fixed") strOptions="height="+strHeight+",width="+strWidth;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location";
	if (strType=="applyonline") strOptions="scrollbars,status,resizable";
	// position
	var left = (screen.width/2) - strWidth/2;
 	var top = (screen.height/2) - strHeight/2;
	strOptions +=',width='+strWidth+',height='+strHeight+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	// open
	newWin = window.open(strURL, 'newWin', strOptions);
	newWin.focus();
}

// pop-up for opening news and project images
var popwin = null;
function popImage(imagesrc,imgwidth,imgheight,alt) {	
	var maxWidth = screen.width - 30;
	var maxHeight = screen.height - 70;
	var winwidth = (imgwidth) ? imgwidth : 500;
	var winheight = (imgheight) ? imgheight : 500;
	var look = '';
	var left = (screen.width/2) - winwidth/2;
 	var top = (screen.height/2) - winheight/2;
	
	// resize window if too large
	if(imgwidth > maxWidth) {
		winwidth = maxWidth;
		look += 'resizable,scrollbars,';	
		left = 0;
	}
	if(imgheight > maxHeight) {
		winheight = maxHeight;
		if(!look) look += 'resizable,scrollbars,';
		top = 0;
	}
	
	look +='width='+winwidth+',height='+winheight+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	if(alt==null) alt = 'Image';
	if (popwin != null && !popwin.closed) popwin.close();
	popwin=window.open("","imgWin",look);
	popwin.document.open();
	popwin.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n');
	popwin.document.write('<html>\n<head>\n');
	popwin.document.write('<title>'+alt+'</title>\n');
	popwin.document.write('<style type="text/css">\n');
	popwin.document.write('body { margin:0; padding:0; background-color:black; }\n');
	popwin.document.write('img { display:block; border:none; }\n');
	popwin.document.write('</style>\n');
	popwin.document.write('</head>\n<body>\n');
	popwin.document.write('<a href="javascript:;" alt="close" onclick="window.close()">');
	popwin.document.write('<img src="'+imagesrc+'" alt="'+alt+'"');
	if(imgwidth) popwin.document.write(' width="'+imgwidth+'"');
	if(imgheight) popwin.document.write(' height="'+imgheight+'"');
	popwin.document.write('></a>\n');
	popwin.document.write('</body>\n</html>');
	popwin.document.close();
	return false;
}

// Form validation stuff by KW
// Function that returns true if a string contains only white space
function isblank(s) {
	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != " ") && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

// Function to check text has been entered in a field
function chkField(e) {
	if ((e.value == null) || (e.value == "") || (e.value == "Enter keyword") || isblank(e.value) || (e.value.length == 0 )) {
		return false;
	}
	else {
		return true;
	}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_jumpMenuGo(selName,targ,restore){ //v3.0
  var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}