var req;

function importXML(url) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			newsList();
	    } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

function newsList(pubYear)
{	
	yearUrl = document.location.href;
	yearUrl = yearUrl.substring(yearUrl.length-2,yearUrl.length);
	document.getElementById("newsMain").innerHTML = "";
	if (!pubYear)
	{
		if (yearUrl != "sp")
		{
		pubYear = yearUrl;
		} else {
		pubYear = "09";
		}
	}

	//Puts all article tags into variable x
	x = req.responseXML.getElementsByTagName('article');

	for (i=0;i<x.length;i++)

	{
		//Get article link
		mm = x[i].getAttribute("mm");
		dd = x[i].getAttribute("dd");
		yy = x[i].getAttribute("yy");
		sd = x[i].getAttribute("sameday");
		order = x[i].getAttribute("order");
		wholeDate = mm + dd + yy;

		if (sd === "yes")
		{
			switch (wholeDate) {
			case "022707": newsLink = "/pdf/news/q406_earnings.pdf"; break;
			case "110806": newsLink = "/pdf/news/q306_earnings.pdf"; break;
			case "050307": newsLink = "/pdf/news/q107_earnings.pdf"; break;
			case "080807": newsLink = "/pdf/news/q207_earnings.pdf"; break;
			case "110807": newsLink = "/pdf/news/q307_earnings.pdf"; break;
			case "022808": newsLink = "/pdf/news/q407_earnings.pdf"; break;
			case "050808": newsLink = "/pdf/news/q108_earnings.pdf";break;
			case "073108": newsLink = "/pdf/news/q208_earnings.pdf";break;
			case "110608": newsLink = "/pdf/news/q308_earnings.pdf";break;
			case "022609": newsLink = "/pdf/news/q408_earnings.pdf";break;
			case "050709": newsLink = "/pdf/news/q109_earnings.pdf";break;
			case "073009": newsLink = "/pdf/news/q209_earnings.pdf";break;
			case "110309": newsLink = "/pdf/news/q309_earnings.pdf";break;
			default: newsLink = "/about/news/article.jsp?d=" + wholeDate + "a";
			} 
		} else if (sd === "third")
		{
			newsLink = "/about/news/article.jsp?d=" + wholeDate + "b";
		}else {
			newsLink = "/about/news/article.jsp?d=" + wholeDate;
		}

		//Convert numerical month value into month names
		switch (mm) {
			case "01":
			var month = "January";
			break;
			
			case "02":
			var month = "February";
			break;
			
			case "03":
			var month = "March";
			break;
			
			case "04":
			var month = "April";
			break;
			
			case "05":
			var month = "May";
			break;
			
			case "06":
			var month = "June";
			break;
			
			case "07":
			var month = "July";
			break;
			
			case "08":
			var month = "August";
			break;
			
			case "09":
			var month = "September";
			break;
			
			case "10":
			var month = "October";
			break;
			
			case "11":
			var month = "November";
			break;
			
			case "12":
			var month = "December";
			break;

}
		
		//Get rid of the leading zero for the day

		day = dd;
		if (day.charAt(0) === "0")
		{
			day = day.charAt(1);
		} 

		year = "20" + yy;

		if (wholeDate === "111108")
		{
			date = "November 10, 2008";
		} else {

			date = month + " " + day + ", " + year;
		}

		var container = document.createElement('div');
		
		//Set last div to have no bottom border
		if (order === "last"){container.setAttribute('style','border:0');}
		
		if (yy === pubYear) {
		
			for (j=0;j<x[i].childNodes.length;j++)	{

				//This line deals with Mozilla counting space as the first node
				if (x[i].childNodes[j].nodeType != 1) continue;
			
				//Assign the data in this array node to a variable				
				var theData = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);

				if (x[i].childNodes[j].nodeName === "headline")
				{
					var anchor = document.createElement('a');
					anchor.setAttribute('href',newsLink);
					anchor.appendChild(theData);
					var brk = document.createElement('br');
					container.appendChild(anchor);
					container.appendChild(brk);
				}			

				if (x[i].childNodes[j].nodeName === "location")
				{
					var loc = document.createElement('b');
					theData.appendData(" -- ");
					theData.appendData(date);
					theData.appendData(" -- ");
					loc.appendChild(theData);
					container.appendChild(loc);
					
				}

				if (x[i].childNodes[j].nodeName === "synopsis")
				{
					container.appendChild(theData);
				}
			
			}
		
		document.getElementById('newsMain').appendChild(container);
			
		}
	}	
		
}

