function loadXMLDoc(dname)   //cross-browser function to load an XML document, "dname"
{
   try //Internet Explorer
   {
       xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
   }
   catch(e)
   {
      try //Firefox, Mozilla, Opera, etc.
      {
         xmlDoc=document.implementation.createDocument("","",null);
      }
      catch(e) {alert(e.message)}
   }
   try
   {
       var ua = navigator.userAgent.toLowerCase();
       if (ua.indexOf('safari/') != -1){ //user is reporting as Safari, use XMLHttpRequest instead.
          XmlHTTP = new XMLHttpRequest();
          XmlHTTP.open("get",dname, false);
          XmlHTTP.send("");
          var xDoc = XmlHTTP.responseXML;
          return xDoc;
       }else{
         xmlDoc.async=false;
         xmlDoc.load(dname);
         return(xmlDoc); 
      }
   }
   catch(e) {alert(e.message)}
   return(null);
} 

function parseNewsXML(news_xml, year_str) {
    xmlDoc = loadXMLDoc(news_xml);
    if(xmlDoc == null) {
	return;
    }
    document.writeln("<table class='news'>");
    var posts = xmlDoc.getElementsByTagName("Post");
    for(i=0;i<posts.length;i++) {
	var year_elem = posts[i].getElementsByTagName("Year")[0].childNodes[0].nodeValue;
	
	if(year_str == "2009") {
	    if(!year_elem.match("2009")) {
		continue;
	    }
	} else if(year_str == "2008") {
	    if(!year_elem.match("2008")) {
		continue;
	    }
	} else if(year_str == "2007") {
	    if(!year_elem.match("2007")) {
		continue;
	    }
	} else if(year_str == "2006") {
	    if(!year_elem.match("2006")) {
		continue;
	    }
        } else {
	    if(year_elem.match("2009") || year_elem.match("2008") || 
	       year_elem.match("2007") || year_elem.match("2006")) {
		continue;
	    }
	}

	document.writeln("<tr>");
	document.writeln("<td class='newsDate'>");
	document.writeln("\["+posts[i].getElementsByTagName("Month")[0].childNodes[0].nodeValue+" "+
			 posts[i].getElementsByTagName("Day")[0].childNodes[0].nodeValue+" "+year_elem+"\]");
	document.writeln("</td>");
	document.writeln("<td class='newsDetails'>");
	document.writeln(posts[i].getElementsByTagName("Content")[0].childNodes[0].nodeValue);
	document.writeln("</td>");
	document.writeln("</tr>");
    }
    document.write("</table>");

}

function parseEventsXML(events_xml, type) {
    xmlDoc = loadXMLDoc(events_xml);
    if(xmlDoc == null) {
	return;
    }
    var events = xmlDoc.getElementsByTagName("Event");
    document.writeln("<table class='event'>");
    for(i=0;i<events.length;i++) {
	var type_elem = events[i].getElementsByTagName("Type")[0].childNodes[0].nodeValue;
	if(type == "conferences") {
	    if(!type_elem.match("Conference")) {
		continue;
	    }
	} else if(type == "competitions") {
	    if(!type_elem.match("Competition")) {
		continue;
	    }
	}
	document.writeln("<tr>");
	document.writeln("<td class='eventURL'>");
	document.write("<a href=\""+events[i].getElementsByTagName("URL")[0].childNodes[0].nodeValue+"\">");
	document.write(events[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue+"</a>");
	document.writeln("</td>");
	document.writeln("<td class='eventDetails'>");
	document.writeln(events[i].getElementsByTagName("Details")[0].childNodes[0].nodeValue);
	document.writeln("</td>");
	document.writeln("</tr>");
    }
    document.write("</table>");
}

function parsePublicationsXML(publications_xml) {
    xmlDoc = loadXMLDoc(publications_xml);
    if(xmlDoc == null) {
	return;
    }
    var publications = xmlDoc.getElementsByTagName("Publication");
    document.writeln("<ul class='publications'>");
    for(i=0;i<publications.length;i++) {	
	document.writeln("<p>");
	document.writeln("<li class='publicationTitle'>");
	document.writeln(publications[i].getElementsByTagName("Title")[0].childNodes[0].nodeValue);
	document.writeln("</li>");
	document.writeln("<ul class='publicationDetails'>");
	document.writeln("<li class='publicationDetails'>");
	document.writeln("Authors: "+publications[i].getElementsByTagName("Authors")[0].childNodes[0].nodeValue);
	document.writeln("</li>");
	document.writeln("<li class='publicationDetails'>");
	document.writeln("Purpose: "+publications[i].getElementsByTagName("Purpose")[0].childNodes[0].nodeValue);
	document.writeln("</li>");
	document.writeln("<li class='publicationDetails'>");
	document.writeln("Date: "+publications[i].getElementsByTagName("Date")[0].childNodes[0].nodeValue);
	document.writeln("</li>");
	document.writeln("<li class='publicationAbstract'>");
	document.writeln("<a href='#' id='abstract"+i+"-show' class='abstract' onclick='showHide(\"abstract"+i+
			 "\");return false;'>Show Abstract.</a>");
	document.writeln("<div id='abstract"+i+"' class='abstract'>");
	document.writeln(publications[i].getElementsByTagName("Abstract")[0].childNodes[0].nodeValue);
	document.writeln("<p><a href='#' id='abstract"+i+"-hide' class='abstract' onclick='showHide(\"abstract"+i
			 +"\"); return false;'>Hide Abstract.</a></p>");
	document.writeln("</div>");
	document.writeln("</li>");
	document.writeln("<li class='publicationDetails'>");
	document.writeln("<a href='publications/"+publications[i].getElementsByTagName("PDF")[0].childNodes[0].nodeValue+"'>[PDF]</a>");
	document.writeln("</li>");
	document.writeln("</ul>");
	document.write("</p>");
    }
    document.write("</ul>");
}


function parsePressXML(press_xml) {
    xmlDoc = loadXMLDoc(press_xml);
    if(xmlDoc == null) {
	return;
    }
    var articles = xmlDoc.getElementsByTagName("Article");
    document.writeln("<ul class='articles'>");
    for(i=0;i<articles.length;i++) {	
	document.writeln("<p>");
	document.writeln("<li class='articleTitle'>");
	document.writeln(articles[i].getElementsByTagName("Title")[0].childNodes[0].nodeValue);
	document.writeln("</li>");
	document.writeln("<ul class='article'>");
	document.writeln("<li class='articleDetails'>");
	document.writeln("Source: "+articles[i].getElementsByTagName("Source")[0].childNodes[0].nodeValue);
	document.writeln("</li>");
	document.writeln("<li class='articleDetails'>");
	document.writeln("URL: <a href='"+articles[i].getElementsByTagName("URL")[0].childNodes[0].nodeValue+
			 "'>"+articles[i].getElementsByTagName("URL")[0].childNodes[0].nodeValue+"</a>");
	document.writeln("</li>");
	document.writeln("<li class='articleDetails'>");
	document.writeln("Summary: "+articles[i].getElementsByTagName("Summary")[0].childNodes[0].nodeValue);
	document.writeln("</li>");
	document.writeln("</ul>");
	document.write("</p>");
    }
    document.write("</ul>");
}

function parseLinksXML(links_xml) {
    xmlDoc = loadXMLDoc(links_xml);
    if(xmlDoc == null) {
	return;
    }
    var links = xmlDoc.getElementsByTagName("Link");
    document.writeln("<table class='link'>");
    for(i=0;i<links.length;i++) {
	document.writeln("<tr>");
	document.writeln("<td class='linkUrl'>");
	document.write("<a href=\""+links[i].getElementsByTagName("URL")[0].childNodes[0].nodeValue+"\">");
	document.write(links[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue+"</a>");
	document.writeln("</td>");
	document.writeln("</tr>");
	document.writeln("<tr class='link'>");
	document.writeln("<td class='linkDetails'>");
	document.writeln(links[i].getElementsByTagName("Description")[0].childNodes[0].nodeValue);
	document.writeln("</td>");
	document.writeln("</tr>");
    }
    document.write("</table>");
}

function parseMembersXML(members_xml) {
    xmlDoc = loadXMLDoc(members_xml);
    if(xmlDoc == null) {
	return;
    }
    var members = xmlDoc.getElementsByTagName("Member");
    for(i=0;i<members.length;i++) {
	var member_name = members[i].getElementsByTagName("Name");
	var member_position = members[i].getElementsByTagName("Position");
        var member_id = members[i].getElementsByTagName("UNIX_ID");
        var member_url = members[i].getElementsByTagName("Member_URL");
	if(member_name.length > 0) {
            document.writeln("<ul class='members' style='list-style-image:url(images/bullet-"+(i%4)+".gif)'>");
	    document.writeln("<p>");
	    if(member_url.length > 0) {
		document.writeln("<li class='member'>");	       
	        document.writeln("<a href="+member_url[0].childNodes[0].nodeValue+">"+
		  	        member_name[0].childNodes[0].nodeValue+"</a>");
		document.writeln("</li>");	    
	    } else {
                document.writeln("<li class='member_name'>");
		document.writeln(member_name[0].childNodes[0].nodeValue);
		document.writeln("</li>");	    
	    }			
	    document.writeln("<ul class='member'>");
	    if(member_position.length > 0) {	        
		document.writeln("<li class='member'>");
		document.writeln(member_position[0].childNodes[0].nodeValue);
		document.writeln("</li>");
	    }
	    if(member_id.length > 0) {
	        document.writeln("<li class='member'>");
		document.writeln("<a class='email' href='mailto:"+member_id[0].childNodes[0].nodeValue+
				     "-at-cs.ualberta.ca'>"+member_id[0].childNodes[0].nodeValue+
				     " at cs dot ualberta dot ca </a>");
		document.writeln("</li>");
            }
	    document.writeln("</ul>");
	    document.write("</p>");
	    document.write("</ul>");
	}
    }
}

