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) {
			article();
	    } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

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

	//Get value of d by getting the last six characters of the url string
	storyHref = document.location.href;

	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");

		if (sd === "yes") {
			articleDate = mm + dd + yy + "a";
			storyUrl = storyHref.substring(storyHref.length-7);
		} else if (sd === "third") {
			articleDate = mm + dd + yy + "b";
			storyUrl = storyHref.substring(storyHref.length-7);
		} else {
			articleDate = mm + dd + yy;
			storyUrl = storyHref.substring(storyHref.length-6);
		}
		
		articleDateUrl = "/about/news/articles/" + articleDate + ".html";

		if(storyUrl === articleDate) {
		document.getElementById('article').innerHTML = "";
		
			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 mainHead = document.createElement('h2');
					mainHead.setAttribute('class','dark');
					mainHead.setAttribute('className','dark');
					mainHead.appendChild(theData);
				}			
				if (x[i].childNodes[j].nodeName == "subhead")
				{
					
					subheadNumber = x[i].childNodes[j].getAttribute("subheadId");
					if (subheadNumber == "1")
					{
						var italHead1 = document.createElement('p');
						italHead1.setAttribute('class','italic');
						italHead1.setAttribute('className','italic');
						italHead1.appendChild(theData);
					}
					else if (subheadNumber == "2")
					{
						var italHead2 = document.createElement('p');
						italHead2.setAttribute('class','italic');
						italHead2.setAttribute('className','italic');
						italHead2.appendChild(theData);
					}

					else {
						var italHead3 = document.createElement('p');
						italHead3.setAttribute('class','italic');
						italHead3.setAttribute('className','italic');
						italHead3.appendChild(theData);
					}
										
				}			
								
			}
			
		
		function loadpage(page_request){
			if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
			try{document.getElementById('articleText').innerHTML=page_request.responseText;} catch(e){}
		}

		var page_request = false
			if (window.XMLHttpRequest) { // if Mozilla, Safari etc
				page_request = new XMLHttpRequest()
			}
			else if (window.ActiveXObject){ // if IE
				try {
				page_request = new ActiveXObject("Msxml2.XMLHTTP")
				} 
				catch (e){
				try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
				}
				catch (e){}
				}
			}
			else {
				return false
			}
			page_request.onreadystatechange=function(){
				loadpage(page_request)
			}
			
			page_request.open('GET', articleDateUrl, true)
			page_request.send(null)
			
			

		}
			
		try{document.getElementById('article').appendChild(mainHead);} catch(e){}
		if (italHead1) document.getElementById('article').appendChild(italHead1);
		if (italHead2) document.getElementById('article').appendChild(italHead2);
		if (italHead3) document.getElementById('article').appendChild(italHead3);	
				
	}
		
}










