// UTILITY FUNCTIONS, ALSO USED IN EXTERNAL FILES
//------------------------------------------------------------------------------

if(Array.prototype.push == null)		// enables array.push for old browsers, e.g. IE5. Used by EventCache var below
{
	Array.prototype.push = function()
	{
		for(var i = 0; i < arguments.length; i++)
		{
			this[this.length] = arguments[i];
		};
		return this.length;
	};
};

var EventCache = function()	// fix for IE memory leak problem with event handling; relies on 'array.push' being enabled
{
	var listEvents = [];
	return {
			listEvents : listEvents,

			add : function(node, sEventName, fHandler, bCapture)
				{
				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]);
						};

					/* From this point on we need the event names to be prefixed with 'on" */
					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;
				};
			}
		};
}();

function addEvent(obj, type, fn) // - requires above EventCache var
{
	if(obj.addEventListener)
		{obj.addEventListener(type, fn, false);}
	else
	{
		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);
}

function removeEvent(obj, type, fn)
{
	if (obj.detachEvent)
		{
		obj.detachEvent("on"+type, fn );
		obj[type+fn] = null;
		}
	else
		{obj.removeEventListener( type, fn, false );}
}

function getObject(objname, type)
{
	if(type == 'id')
		{DOMchunk = document.getElementById(objname)}
	if(type == 'tag')
		{DOMchunk = document.getElementsByTagName(objname)}
	return DOMchunk
}

function filter(coll, filter, newArray, type, attname) // filters by id, class or attribute - collection, filter string, new collection, type, attribute name
{
    for(var i=0; i<coll.length; i++)
        {
        var pattern = new RegExp(filter);

        if (type=='tag')
            {
            if (pattern.test(coll[i].className))
                {newArray[newArray.length] = coll[i]}
            }
        if (type=='id')
            {
            if (pattern.test(coll[i].id))
                {newArray[newArray.length] = coll[i]}
            }
        if (type=='att')
            {
            if (pattern.test(coll[i].getAttribute(attname)))
                {newArray[newArray.length] = coll[i]}
            }
        }
    return newArray;
}

function addClass()					// adding a class; needs a .newClass property added by the calling function
{
	if(this.className)
		{
		currentClass = this.className;
		}
	else
		{currentClass = ''}
	this.className = (currentClass + ' ' + this.newClass);
}

function removeClass()
{
	var str = this.className;
	str = str.replace(this.newClass,'');
	this.className = str;
}

function createXMLHttpRequest()		// generic asynchronous requests
{
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
	try { return new XMLHttpRequest(); } catch(e) {}
	return null;
}

/* Array prototype, matches value in array: returns bool */
/* example:  bong = new Array('thing', 'fat', 'spow')
	if (bong.inArray('fat'))
		{alert('yes')}
	else
		{alert('no')}*/

Array.prototype.inArray = function (value)
{
	var i;
	for (i=(this.length-1); i>=0; i--)
		{
		if (this[i] === value) 	// nb triple-equals means 'identical to'
			{
			return true;
			}
		}
	return false;
}

function insertAfter(parent, node, referenceNode)	/* insert an element after a particular node */
{
	parent.insertBefore(node, referenceNode.nextSibling);
}

function setCookie(name, value, expiredays)
{
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

	document.cookie = name + "=" + escape(value) + "; path=/" +
	((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}