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;
			};
		}
	};
}();


/* ------------------------------------------------
  PVII Equal CSS Columns scripts - Version 2
  Copyright (c) 2005 Project Seven Development
  www.projectseven.com
  Version: 2.1.0
------------------------------------------------ */
function P7_colH2(){ //v2.1.0 by PVII-www.projectseven.com
	var i,oh,h=0,tg,el,np,dA=document.p7eqc,an=document.p7eqa;if(dA&&dA.length){
	for(i=1;i<dA.length;i+=2){dA[i+1].style.paddingBottom='';}for(i=1;i<dA.length;i+=2){
	oh=dA[i].offsetHeight;h=(oh>h)?oh:h;}for(i=1;i<dA.length;i+=2){oh=dA[i].offsetHeight;
	if(oh<h){np=h-oh;if(!an&&dA[0]==1){P7_eqA2(dA[i+1].id,0,np);}else{
	dA[i+1].style.paddingBottom=np+"px";}}}document.p7eqa=1;
	document.p7eqth=document.body.offsetHeight;
	document.p7eqtw=document.body.offsetWidth;}
}
function P7_eqT2(){ //v2.1.0 by PVII-www.projectseven.com
	if(document.p7eqth!=document.body.offsetHeight||document.p7eqtw!=document.body.offsetWidth){P7_colH2();}
}
function P7_equalCols2(){ //v2.1.0 by PVII-www.projectseven.com
	var c,e,el;if(document.getElementById){document.p7eqc=new Array();
	document.p7eqc[0]=arguments[0];for(i=1;i<arguments.length;i+=2){el=null;
	c=document.getElementById(arguments[i]);if(c){e=c.getElementsByTagName(arguments[i+1]);
	if(e){el=e[e.length-1];if(!el.id){el.id="p7eq"+i;}}}if(c&&el){
	document.p7eqc[document.p7eqc.length]=c;document.p7eqc[document.p7eqc.length]=el}}
	setInterval("P7_eqT2()",10);}
}
function P7_eqA2(el,p,pt){ //v2.1.0 by PVII-www.projectseven.com
	var sp=10,inc=20,g=document.getElementById(el);np=(p>=pt)?pt:p;
	g.style.paddingBottom=np+"px";if(np<pt){np+=inc;
	setTimeout("P7_eqA2('"+el+"',"+np+","+pt+")",sp);}
}


/*************************************************************************************
 * When a label receives focus, whether it be through onclick or through accessKeys,
 * the associated element specified in the labels for="" should receive focus also.
 *************************************************************************************/
function initLabels() {
	labels = document.getElementsByTagName("label");
	for(i = 0; i < labels.length; i++) {
		addEvent(labels[i], "click", labelFocus);
	}
}
function labelFocus() {
	new Field.focus(this.getAttribute('for'));
}


/*
Unobtrusive External Links (just add rel="external" to links)
http://www.sitepoint.com/article/standards-compliant-world/3
*/
function externalLinks() {
	if (!document.getElementsByTagName) {
		return;
	}
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		if (anchors[i].getAttribute("href") && anchors[i].getAttribute("rel") == "external") {
			anchors[i].target = "_blank";
		}
	}
}

/* --------------------------------------------------
  RowLite (table row highlighter) script
  Copyright (c) 2005-2006 Project Seven Development
  www.projectseven.com
  Version: 1.1 by Javier Julio - allows alt class
  		   1.0 by PVII - table row highlighter
--------------------------------------------------- */
function P7_rowLite(tb,cl){	//v1.1
	var g,i,x,gr,a;if(document.getElementById){g=document.getElementsByTagName("TABLE");
	for(x=0;x<g.length;x++){if(g[x].className&&g[x].className==tb){
	gr=g[x].getElementsByTagName("TR");if(gr){for(i=0;i<gr.length;i++){
	if(i>0){gr[i].onmouseover=function(){a=this.className;this.className=cl;};
	gr[i].onmouseout=function(){this.className=a;};}}}}}}
}

/* CF JavaScript style functions - Find, FindNoCase, ListFind, ListFindNoCase */
function Find(sb,s) {
	 return s.toString().indexOf(sb) + 1;
}
function FindNoCase(sb,s) {		
	return Find(sb.toUpperCase(),s.toUpperCase());
}
function ListAppend(l, v, d) {
	if(!d){d = ",";}
	var r = "";
	if (ListLen(l,d) && l != "") {
		r = l + d + v;
	} else {
		r = v;
	}
	return r;
}
function ListFind(l,v,d) {
	if(!d){d = ",";}
	var r = 0;
	var listToArray = l.split(d);
	for (var i=0; i < listToArray.length; i++) {		
		if (listToArray[i] == v) {
			r = i + 1;
			break;
		}
	}
	return r;
}
function ListFindNoCase(l,v,d) {
	if(!d){d = ",";}
	return ListFind(l.toUpperCase(), v.toUpperCase(), d);
}
function ListLen(l,d) {			
	if(!d){d = ",";}		
	return l.split(d).length;
}

/* 
	remove optional form fields 
	add to page: <'tagName' id="frmOptional"></'tagName'>
*/
function removeOptional() {
	if(document.getElementById) {
		var linkContainer = document.getElementById('frmOptional');
		if (linkContainer) {
			var toggle = linkContainer.appendChild(document.createElement('a'));
			toggle.href = '#';
			toggle.appendChild(document.createTextNode('Hide optional fields?'));
			toggle.onclick = function() {
				var linkText = this.firstChild.nodeValue;
				this.firstChild.nodeValue = (linkText == 'Hide optional fields?') ? 'Display optional fields?' : 'Hide optional fields?';
				var tmp = document.getElementsByTagName('div');
				for (var i=0;i<tmp.length;i++) {
					if(ListFindNoCase(tmp[i].className, 'optional', ' ')) {
						tmp[i].style.display = (tmp[i].style.display == 'none') ? 'block' : 'none';
					}
				}
				return false;
			}
		}
	}
}

/* action shortcut (for tables) */
function performAction() {
	var objAction = document.getElementById("actions");
	var objMark = document.getElementsByTagName("input");
	var selectedOption = objAction.options[objAction.selectedIndex].value;
	var idList = "";

	if (FindNoCase("delete", selectedOption)) {
		if (confirm("Are you sure you wish to delete the selected records?") == false) {
			return false;
		}
	}

	for (i = 0; i < objMark.length; i++) {
		if (objMark[i].checked && objMark[i].type == "checkbox" && FindNoCase("action", selectedOption)) {
			idList = ListAppend(idList, objMark[i].value);
		} else if (objMark[i].checked && objMark[i].type == "checkbox") {
			idList = objMark[i].value;
			break;
		}
	}

	if (objAction.selectedIndex && idList != "") {
		if (FindNoCase("action", selectedOption)) {
			window.location.href = selectedOption + "&idList=" + idList;
		} else {
			window.location.href = selectedOption + idList;
		}
	}
}

/* onload for all pages */
function pageLoader() {
	externalLinks();
	initLabels();
	P7_rowLite('highlight', 'hover');
	removeOptional();
}

addEvent(window, 'unload', EventCache.flush);
addEvent(window, 'load', pageLoader);