

/* 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) {
	if(!func) return;
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			eval( func + "();");
		}
	}
}

/* grab Elements from the DOM by className */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* toggle an element's display */
function toggle(obj,inline) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

function hide(obj) { document.getElementById(obj).display="none"; alert("NO"); }
function show(obj) { document.getElementById(obj).display="block"; alert("YES"); }

/* insert an element after a particular node */
function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}

/* get, set, and delete cookies */
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
	
function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}
	
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/*
-------------------------------------------------------
The following function is not from the common library, 
but works with the above (3) functions...
-------------------------------------------------------
*/

function testCookie()
{
	var gCookieName; 
	var gCookieValueRet;
	gCookieName="TestCookie";
	gCookieValue="Testing123";
	gCookieValueRet="Return"
	var test=getCookie(gCookieName);
	if (test==null) {
	setCookie(gCookieName,gCookieValue,1);
	gCookieValueRet=getCookie(gCookieName);
	
	if (gCookieValueRet==null) {
		var warning = '<p style=\"padding: 10px 50px;\" class=\"error\"><strong>Warning!</strong><br />You must have cookies enabled for this page to operate. Enable your browsers cookies and <a href=\"'+window.location+'\">reload this page</a>.</p>';
		document.getElementById( 'maincolumn' ).innerHTML = warning; 
		//alert(warning);
		}
	}
}

/* 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;
}

/*
=======================================
THE ABOVE FUNCTIONS ARE FROM COMMON.JS
http://www.dustindiaz.com/top-ten-javascript/
=======================================
*/

function changeTab(tab)
{
	var temp;
	var tabs = getElementsByClass("panel");
	for(i=0;i<tabs.length;i++)
	{
		temp = tabs[i].id;
		if(temp==(tab+'_panel')) {
			tabs[i].style.display="block";
			tabs[i].style.className="";
			document.getElementById(temp.replace(/panel/,"tab")).className="here";
		} else {
			tabs[i].style.display="none";
			tabs[i].style.className="panel";
			document.getElementById(temp.replace(/panel/,"tab")).className="there";
		}
	}
}



function noLinkFocus()
{
	var links = document.links;			
	for (var i=0; i<links.length; i++) {
			links[i].onfocus=function() { if(this.blur) this.blur(); }
	}
}

addLoadEvent(noLinkFocus);


function validEmail(email)
{
	if(email.length <= 0)
	{
		return true;
	}
	var splitted = email.match("^(.+)@(.+)$");
	if(splitted == null) return false;
	if(splitted[1] != null )
	{
		var regexp_user=/^\"?[\w-_\.]*\"?$/;
		if(splitted[1].match(regexp_user) == null) return false;
	}
	if(splitted[2] != null)
	{
		var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
		if(splitted[2].match(regexp_domain) == null) 
		{
			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if(splitted[2].match(regexp_ip) == null) return false;
		}// if
		return true;
	}
	return false;
}


function popup(url,w,h) { popUp(url,w,h); void(0); }
function popUp(url,w,h)
{
	if(!h) var h = screen.height * .75;
	if(!w) var w = screen.width * .75;
	var t = (screen.height/2)-(h/1.5);
	var l = (screen.width/2)-(w/2);
	newwindow=window.open( url ,'',
	'directories=0'+
	',fullscreen=0'+
	',height='+h+
	',width='+w+
	',left='+l+
	',top='+t+
	',location=0'+
	',menubar=0'+
	',resizable=1'+
	',scrollbars=1'+
	',status=0'+
	',titlebar=0'+
	',toolbar=0');
	if (window.focus) {newwindow.focus()}
	return false;
}



function togRow(id) {
	objRow = document.getElementById(id);
	if (navigator.appName == 'Opera') {
		if (objRow.style.display == 'table-row') {
			objRow.style.visibility = 'hidden';
			objRow.style.display = 'none';
			objRow.style.position = 'absolute';
		}else{
			objRow.style.visibility = 'visible';
			objRow.style.display = 'table-row';
			objRow.style.position = 'relative';
		}
	}else{
		if (objRow.style.visibility == 'visible') {
			objRow.style.visibility = 'hidden';
			objRow.style.display = 'none';
			objRow.style.position = 'absolute';
		}else{
			objRow.style.visibility = 'visible';
			objRow.style.display = '';
			objRow.style.position = 'relative';
		}
	}
}


function checkUncheckAll(theElement) 
{
	var theForm = theElement.form, z = 0;
	for(z=0; z<theForm.length;z++)
	{
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
		{
			theForm[z].checked = theElement.checked;
		}
	}
}



function popContent(con,w,h)
{
	if(!h) var h = screen.height * .75;
	if(!w) var w = screen.width * .75;
	var t = (screen.height/2)-(h/1.5);
	var l = (screen.width/2)-(w/2);
	var name = '';
	newwindow=window.open(null,name,
	'directories=0'+
	',fullscreen=0'+
	',height='+h+
	',width='+w+
	',left='+l+
	',top='+t+
	',location=0'+
	',menubar=0'+
	',resizable=1'+
	',scrollbars=1'+
	',status=0'+
	',titlebar=0'+
	',toolbar=0');

	newwindow.document.write('<html><head><title></title></head><body>');
	newwindow.document.write(con);
	newwindow.document.write('</body></html>');
	newwindow.document.close();

	if (window.focus) {newwindow.focus()}
	void(0);
}



function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}
