function GetXmlHttpObject() {
 var xmlHttp=null;
 try {
   // Firefox, Opera 8.0+, Safari
   xmlHttp=new XMLHttpRequest();
 } catch (e) {
   // Internet Explorer
   try {
     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
     xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
 }
 return xmlHttp;
}

function myPreventDefault(e) {
	var ev=(!e)?window.event:e;//IE:Moz
	ev.preventDefault?ev.preventDefault():ev.returnValue = false;
}

function myStopBubble(e) {
	var ev=(!e)?window.event:e;//IE:Moz
	ev.stopPropagation?ev.stopPropagation():ev.cancelBubble = true;
}

function prepareURL(prefix,pairs,cache) {
	
	//create a query string from the url
	url = prefix+"?";
	for(key in pairs) {
		//encode all the parameters to be paased
		url = url+"&"+key+"="+escape(pairs[key]);
	}

	if(cache == false) {
		//prevent caching of ajax calls
		t = new Date();
		url = url+"&t="+t.getTime();
	}
	
	return url;
}

function convertToHtml(xml,parent) {
	var i;
	for(i=0;i<xml.childNodes.length;i++) {
		//create the HTML node
		if(xml.childNodes[i].nodeName == "#text") {
			theHtmlNode = document.createTextNode(xml.childNodes[i].nodeValue);
		} else {
			theHtmlNode = document.createElement(xml.childNodes[i].nodeName);
		}

		//get the attributes
		if(xml.childNodes[i].attributes) {
			for(m=0;m<xml.childNodes[i].attributes.length;m++) {
				if(xml.childNodes[i].attributes[m].name == 'class') {
					theHtmlNode.className = xml.childNodes[i].attributes[m].value;
				} else if(xml.childNodes[i].attributes[m].name.match(/^on/)) {
					var theListenerString = xml.childNodes[i].attributes[m].value.match(/(.*)\((.*)\);?/);
					var funcName = theListenerString[1];
					var funcArgs = theListenerString[2];
					
					var theFunctionString = funcName + "(e," + funcArgs + ");";
					
					if(theHtmlNode.addEventListener) {
						theHtmlNode.addEventListener(xml.childNodes[i].attributes[m].name.replace(/^on/,''),function(e) { eval(theFunctionString); },false);
					} else {
						theHtmlNode.attachEvent(xml.childNodes[i].attributes[m].name,function(e) { eval(theFunctionString); });
					}
				} else if(xml.childNodes[i].attributes[m].name == 'style') {
					theHtmlNode.style.cssText = xml.childNodes[i].attributes[m].value;
				} else {
					theHtmlNode.setAttribute(xml.childNodes[i].attributes[m].name,xml.childNodes[i].attributes[m].value);
				}
			}
		}
		
		if(xml.childNodes[i].hasChildNodes()) {
			//append the newly created node to the parent and then create the next child by running the function again
			parent.appendChild(theHtmlNode);
			convertToHtml(xml.childNodes[i],theHtmlNode);
		} else {
			//append the newly created node to the parent
			parent.appendChild(theHtmlNode);
		}
		
	}
}

function loadWeeksEvents(e,timestamp,type) {
	myPreventDefault(e);
	//make an ajax call to load up the week's events in #eventbox
		var eventReq = GetXmlHttpObject();
			var queryString = new Object();
				queryString['func'] = "showweeksevents";
				queryString['ajax'] = "true";
				queryString['timestamp'] = timestamp;
				queryString['type'] = type;
				
			url = prepareURL('php/scripts/sharedfuncs.php',queryString,false);
			eventReq.open('GET',url,true);
			eventReq.onreadystatechange = function() {
				if(eventReq.readyState == 4 && eventReq.status == 200) {
					xml = eventReq.responseXML.documentElement;
					if(!xml) {
						//reload the page
						window.location.href = window.location.href+'&date='+timestamp;
					} else {
						eventBox = document.getElementById('eventbox');
						while(eventBox.firstChild) {
							eventBox.removeChild(eventBox.firstChild);
						}
						switchBoxes(e,document.getElementById('switcher').getElementsByTagName('LI')[0]);
						convertToHtml(xml,eventBox);
					}
				}
			}
			eventReq.send(null);
}