function openClass(parentID, childClass) {

	$$('#'+parentID+' .'+childClass).each(function(el) {

		if(el == null) return false;

		if(el.style.display == '') return false; //skip displayed ones

		el.style.display = ''; //display the first one
		throw $break; //then stop the iteration

	});

	return false;

}

function openEl(parentID, childEl) {

	$$('#'+parentID+' '+childEl).each(function(el) {

		if(el == null) return false;

		if(el.style.display == '') return false; //skip displayed ones

		el.style.display = ''; //display the first one
		throw $break; //then stop the iteration

	});

	return false;

}

function windowopen(url, vars, addBase) {

	if(vars == null) vars = 'width=10,height=10,resizable,scrollbars';
	if(addBase == null) addBase = true;

	var base = addBase ? document.getElementsByTagName('base')[0].href : '';

	var currentTime = new Date();

	window.open(base + url, currentTime.getTime(), vars);

	return false;

}

function gets(varName) {

	var toReturn = '';

	var urlHalves = String(document.location).split('?');

	if(urlHalves[1]) {
		var urlVars = urlHalves[1].split('&');
		for(i=0; i<=(urlVars.length); i++) {
			if(urlVars[i]){
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					toReturn = urlVarPair[1];
				}
			}
		}
	}

	return toReturn;

}

function R(number) {

	var toReturn = '';

	var location = String(document.location);
	location = location.replace(document.getElementsByTagName('base')[0].href, '');

	var Rs = location.split('/');

	//get rid of the last GET variable if its there
	var last = Rs.last();
	if(last.startsWith('?')) {
		Rs.splice(-1);
	}

	number--;

	if(Rs[number]) {
		return Rs[number];
	} else {
		return '';
	}

}


function cancel() {

	var sure = confirm('Are you sure you want to cancel?');

	if(sure) {
		goBack();
	}

	return false;

}

function forward(page) {

	document.location.href = document.getElementsByTagName('base')[0].href + page;

}




//returns some info about an ajax response
function ajaxTransportText(transport) {

	var toReturn = new Array();

	if(transport.statusText != "OK") {
		toReturn['error'] = true;
		toReturn['text'] = (transport.statusText != "Internal Server Error") ? 'Error with server' : transport.responseText;
	} else {
		toReturn['error'] = false;
		toReturn['text'] = transport.responseText;
	}

	return toReturn;

}




function goBack() {

	history.go(-1);

	return false;

}



// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini
// http://dean.edwards.name/weblog/2005/10/add-event/

// had also tried http://ecmascript.stchur.com/2006/10/12/fixing-ies-attachevent-failures/
// altho could not do return false on it

//USE e.preventDefault(); to do return false inside it!!

function addEvent(element, type, handler) {

	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = _handleEvent;
	}

};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener) {
		element.removeEventListener(type, handler, false);
	} else {
		// delete the event handler from the hash table
		if (element.events && element.events[type]) {
			delete element.events[type][handler.$$guid];
		}
	}
};

function _handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || _fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$_handleEvent = handlers[i];
		if (this.$$_handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function _fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = _fixEvent.preventDefault;
	event.stopPropagation = _fixEvent.stopPropagation;
	return event;
};
_fixEvent.preventDefault = function() {
	this.returnValue = false;
};
_fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};








// http://24ways.org/2005/splintered-striper

/*
 * Summary:      Core experiment function that applies any number of classes to all child elements
 *               contained in all occurences of a parent element (either with or without a specific class)
 * Parameters:   parentElementTag - parent tag name
 *               parentElementClass - class assigned to the parent; if null, all parentElementTag elements will be affected
 *               childElementTag -  tag name of the child elements to apply the styles to
 *               styleClasses - comma separated list of any number of style classes (using 2 classes gives the classic "zebra" effect)
 * Return:       none
 */

function zebra(parentElementTag, parentElementClass, childElementTag, styleClasses)
{

	var i=0,currentParent,currentChild;
	// capability and sanity check
	if ((document.getElementsByTagName)&&(parentElementTag)&&(childElementTag)&&(styleClasses)) {
		// turn the comma separate list of classes into an array
		var styles = styleClasses.split(',');
		// get an array of all parent tags
		var parentItems = document.getElementsByTagName(parentElementTag);
		// loop through all parent elements
		while (currentParent = parentItems[i++]) {
			// if parentElementClass was null, or if the current parent's class matches the specified class
			if ((parentElementClass == null)||(currentParent.className.indexOf(parentElementClass) > -1)) {
				var j=0,k=0;
				// get all child elements in the current parent element
				var childItems = currentParent.getElementsByTagName(childElementTag);
				// loop through all child elements
				while (currentChild = childItems[j++]) {
					// based on the current element and the number of styles in the array, work out which class to apply
					k = (j+(styles.length-1)) % styles.length;
					// add the class to the child element - if any other classes were already present, they're kept intact
					currentChild.className = currentChild.className+" "+styles[k];
				}
			}
		}
	}
}






//if request takes longer than 1.5 seconds, it shows 'activity'
Ajax.Responders.register({
	onCreate: function() {
		if($('showActivity') && Ajax.activeRequestCount> 0) {
			setTimeout("if(Ajax.activeRequestCount> 0) { showActivity() }", 1500);
		}
	},
	onComplete: function() {
		if($('showActivity') && Ajax.activeRequestCount == 0) {
			hideActivity();
		}
	}
});

function showActivity() {
	if($('showActivity')) {
		Effect.Appear('showActivity',{duration: 0.5, queue: {position:'front', scope:'showActivity', limit: 50}});
	}
}
function hideActivity() {
	if($('showActivity')) {
		Effect.Fade('showActivity',{duration: 0.5, queue: {position:'end', scope:'showActivity', limit: 50}});
	}
}





var Cookie = {
	set: function(name, value, daysToExpire) {
		var expire = '';
		if (daysToExpire != undefined) {
			var d = new Date();
			d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
			expire = '; expires=' + d.toGMTString();
		}
		return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
	},
	get: function(c_name) {

        if (document.cookie.length>0) {

            c_start = document.cookie.indexOf(c_name + "=");

            if(c_start != -1) {
                c_start= c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if(c_end == -1) c_end=document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }

        }

        return "";

	},
	erase: function(name) {
		var cookie = Cookie.get(name) || true;
		Cookie.set(name, '', -1);
		return cookie;
	}
};



//add functions to initAdd to have them run when the DOM is loaded

var inits = new Array();

function initAdd(func) {
	inits[inits.length] = func;
}

var initRun = false;

//call this at the bottom of every page, just before </body>
function init() {

	if(initRun) return false;

	initRun = true;

	//wait 300ms, just so we are sure </body> is done and it is safe
	setTimeout(function() {

		//call all the functions from initAdd()
		for(var i = 0;i < inits.length;i++) {

			inits[i]();

		}

	}, 300);

}

//if we forget to add init() to the end of the page, this will do it!
Event.observe(window, 'load', init);

