// Attaches an event
function addEvent(obj, evType, fn) { 
	if (obj.addEventListener) { 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent) { 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}

// Attaches a style sheet to the current page
function attachStylesheet(linkHref) {
	var linkElement = document.createElement('link');
	linkElement.type = "text/css";
	linkElement.href = linkHref;
	linkElement.rel = "stylesheet";
	var head = document.getElementsByTagName('HEAD');
	head[0].appendChild(linkElement);
}

// Attaches a script to the current page - does not work when attached script uses document.write
/*
function attachScript(scriptSrc) {
	var scriptElement = document.createElement('script');
	scriptElement.type = "text/javascript";
	scriptElement.src = scriptSrc;
	var head = document.getElementsByTagName('BODY');
	head[0].appendChild(scriptElement);
}
*/

// Attaches a script to the current page
// this has to be done thought document.write because the script that it attaches uses document.write to load another script
function attachScript(src) {
var ret='<'+'script src="'+src+'"'+' type="text/javascript"><'+'/script>';
document.write(ret);
}

function getHttpRequest() {
	var http_request = false;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if(!http_request) {
		alert('Giving up. Cannot create an XMLHTTP instance');
		return false;
	} else {
		return http_request;
	}
}

// Removes all of node's child elements
function removeAllChildren(node) {
	while(node.firstChild) {
		node.removeChild(node.firstChild);
	}
}

// Copies all childNodes from source to target
function copyAllChildNodes(source, target) {
	var souceCopy = source.cloneNode(true);
	while(souceCopy.firstChild) {
		target.appendChild(souceCopy.firstChild);
	}
}

// Returns a serialization of the passed xmlNode.
// Up to at least version 2.0, XMLSerializer only works in Safari if you want to get a serialisation of the root node.
function getXMLNodeSerialisation(xmlNode) {
  var text = false;
  try {
    // Gecko-based browsers, Safari, Opera.
    var serializer = new XMLSerializer();
    text = serializer.serializeToString(xmlNode);
  }
  catch (e) {
    try {
      // Internet Explorer.
      text = xmlNode.xml;
    }
    catch (e) {}
  }
  return text;
}

// Returns an array of elements that have a searchClass in their class attribute
// searchClass - the class name to search for
// node - the node to start the search at, if not specified starts with document
// tag - only searches elements of a given tag name - leave blank to search all tags
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 display  of obj between none and whatever is the default
function switchMenu(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

// Shortcut to get at objects.  
// Give it a string, and it returns the object with that id.
// Give it an object, and it returns it.
// Give it multiple things, and it returns them in an array
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;
}
