// JavaScript Document

var xmldoc
var xmlrequest

function getSpecial() {
	var passdata = null;
	var passdata = unescape(location.search.substring(1,location.search.length));
	if (passdata != "") {
		getArticlesByTopic(passdata);
	}
}

function createXMLRequest() {
	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;
        	}
		}
    }
	return req
}

function getAuthors()  {
	//initializes the xml list of articles with the appropriate filtering function "getAuthorsList"
	
	xmlrequest = createXMLRequest();
	if (navigator.appName == "Microsoft Internet Explorer") {
		//IE Version
		xmlrequest.onreadystatechange = function() {
			if (xmlrequest.readyState == 4) {
				getAuthorsList();
			}
		}
	} else {
		//Firefox Version
		xmlrequest.onload = function() {
			getAuthorsList();
		}	
	}
	xmlrequest.open("GET", "publishedArticles.xml", false);
	xmlrequest.send(null);
}

function getAuthorsList()  {
	// returns a list of authors with linkages to retrieve their articles; call getAuthors() to invoke this routine
	//if (document.implementation && document.implementation.createDocument) xmldoc.normalize();
	authors = xmlrequest.responseXML.getElementsByTagName("author");
	
	var authorList1 = new Array();
	var authorList2 = new Array();
	var cnt = 0;
	
	for (a = 0; a < authors.length; a++) {
		authorList1[cnt] = authors[a].firstChild.data;
		cnt++;
	}
	authorList1.sort();
	
	for (c in authorList1) 
		authorList2[authorList1[c]] = c;
		
	for (c in authorList2)
	document.write('<a href="javascript:getArticlesByAuthor(\''+ c + '\');">'+ c + '</a><br/>');
	
}


function getTopics()  {
	//initializes the xml list of articles with the appropriate filtering function "getTopicList"
	xmlrequest = createXMLRequest();
	if (navigator.appName == "Microsoft Internet Explorer") {
		//IE Version
		xmlrequest.onreadystatechange = function() {
			if (xmlrequest.readyState == 4) {
				getTopicList();
			}
		}
	} else {
		//Firefox Version
		xmlrequest.onload = function() {
			getTopicList();
		}	
	}
	xmlrequest.open("GET", "publishedArticles.xml", false);
	xmlrequest.send(null);
}

function getTopicList()  {
	// returns a list of topics with linkages to retrieve their articles; call getTopics() to invoke this function.
	topics = xmlrequest.responseXML.getElementsByTagName("topic");
	
	var list1 = new Array();
	var list2 = new Array();
	var cnt = 0;
	
	for (a = 0; a < topics.length; a++) {
		list1[cnt] = topics[a].firstChild.data;
		cnt++;
	}
	list1.sort();
	
	for (c in list1) 
		list2[list1[c]] = c;
		
	for (c in list2)
	document.write('<a href="javascript:getArticlesByTopic(\''+ c + '\');">'+ c + '</a><br/>');
	
}

function getDates()  {
	//returns a list of dates from the XML datafile with appropriate linkages to each date
	xmlrequest = createXMLRequest();
	if (navigator.appName == "Microsoft Internet Explorer") {
		//IE Version
		xmlrequest.onreadystatechange = function() {
			if (xmlrequest.readyState == 4) {
				getDateList();
			}
		}
	} else {
		//Firefox Version
		xmlrequest.onload = function() {
			getDateList();
		}	
	}
	xmlrequest.open("GET", "publishedArticles.xml", false);
	xmlrequest.send(null);
}

function getDateList()  {
	// private routine called by getDates()
	collection = xmlrequest.responseXML.getElementsByTagName("year");
	
	var list1 = new Array();
	var list2 = new Array();
	var cnt = 0;
	
	for (a = 0; a < collection.length; a++) {
		list1[cnt] = collection[a].firstChild.data;
		cnt++;
	}
	list1.sort();
	
	for (c in list1) 
		list2[list1[c]] = c;
		
	for (c in list2)
	document.write('<a href="javascript:getArticlesByDate(\''+ c + '\');">'+ c + '</a><br/>');
	
}

function getArticlesByAuthor(theAuthor)  {
	
	xmlrequest = createXMLRequest();
	if (navigator.appName == "Microsoft Internet Explorer") {
		//IE Version
		xmlrequest.onreadystatechange = function() {
			if (xmlrequest.readyState == 4) {
				getAuthorXML(theAuthor);
			}
		}
	} else {
		//Firefox Version
		xmlrequest.onload = function() {
			getAuthorXML(theAuthor);
		}	
	}
	xmlrequest.open("GET", "publishedArticles.xml", false);
	xmlrequest.send(null);
}

function getArticlesByDate(theDate)  {
	
	xmlrequest = createXMLRequest();
	if (navigator.appName == "Microsoft Internet Explorer") {
		//IE Version
		xmlrequest.onreadystatechange = function() {
			if (xmlrequest.readyState == 4) {
				getDateXML(theDate);
			}
		}
	} else {
		//Firefox Version
		xmlrequest.onload = function() {
			getDateXML(theDate);
		}	
	}
	xmlrequest.open("GET", "publishedArticles.xml", false);
	xmlrequest.send(null);
}

function getArticlesByTopic(theTopic)  {
	
	xmlrequest = createXMLRequest();
	if (navigator.appName == "Microsoft Internet Explorer") {
		//IE Version
		xmlrequest.onreadystatechange = function() {
			if (xmlrequest.readyState == 4) {
				getTopicXML(theTopic);
			}
		}
	} else {
		//Firefox Version
		xmlrequest.onload = function() {
			getTopicXML(theTopic);
		}	
	}
	xmlrequest.open("GET", "publishedArticles.xml", false);
	xmlrequest.send(null);
}

	
function getAuthorXML(theAuthor)  {
	// returns a page of articles based on theAuthor variable
	articles = xmlrequest.responseXML.getElementsByTagName("article");
	
	//page heading	
	txt = "<link href='../Styles/site.css' rel='stylesheet' type='text/css'>";
	txt += "<h1>Articles By " + theAuthor + "</h1><br/>";
	txt += '<p><a href="#" onclick="javascript:window.location.reload();">Return to list</a></p><br/>';
		
	for (a = 0; a < articles.length; a++)
	{
		//retrieve article record contents
		pub_name = articles[a].getElementsByTagName('name')[0].firstChild.data;
		pub_desc = articles[a].getElementsByTagName('description')[0].firstChild.data;
		pub_year = articles[a].getElementsByTagName('year')[0].firstChild.data;
		pub_mth = articles[a].getElementsByTagName('month')[0].firstChild.data;
		pub_url = articles[a].getElementsByTagName('pub_url')[0].firstChild.data;
		
		//collections of authors
		authors = articles[a].getElementsByTagName("author");
		containsAuthor = false;
		for (t=0; t < authors.length; t++) {
			pub_author = authors[t].firstChild.data;
			if (pub_author == theAuthor) {
				containsAuthor = true;
			}
		}
		
		//filter by author and build page
		if (containsAuthor) {
			txt += '<h3><a href="../resources/articles/'+ pub_url +'" target="_blank">' + pub_name + '</a></h3>';
			txt += '<p style="color: #777777">' + pub_mth + '/' + pub_year + '</p>';
			for (b=0; b < authors.length; b++) {
				pub_author = authors[b].firstChild.data;
				txt += '<p style="color: #777777">' + pub_author + '</p>';
			}
			txt += '<p>' + pub_desc + '</p><br/>';
		}
	}
	txt += '<p><a href="#" onclick="javascript:window.location.reload();">Return to list</a></p>';
	document.getElementById('area').innerHTML = txt;
}

function getDateXML(theDate) {
	// returns a table of articles based on theDate variable
	articles = xmlrequest.responseXML.getElementsByTagName("article");
	
	//page heading
	txt = "<link href='../Styles/site.css' rel='stylesheet' type='text/css'>";
	txt += "<h1>Articles For The Year " + theDate + "</h1><br/>";
	txt += '<p><a href="#" onclick="javascript:window.location.reload();">Return to list</a></p><br/>';
	
	
	for (a = 0; a < articles.length; a++)
	{
		pub_name = articles[a].getElementsByTagName('name')[0].firstChild.data
		pub_desc = articles[a].getElementsByTagName('description')[0].firstChild.data
		pub_year = articles[a].getElementsByTagName('year')[0].firstChild.data
		pub_mth = articles[a].getElementsByTagName('month')[0].firstChild.data
		pub_url = articles[a].getElementsByTagName('pub_url')[0].firstChild.data
		
		//collections of authors for print out
		authors = articles[a].getElementsByTagName("author");
		
		//filter by date and build page
		if (pub_year == theDate) {
			txt += '<h3><a href="../resources/articles/'+ pub_url +'" target="_blank">' + pub_name + '</a></h3>';
			txt += '<p style="color: #777777">' + pub_mth + '/' + pub_year + '</p>';
			for (b=0; b < authors.length; b++) {
				pub_author = authors[b].firstChild.data;
				txt += '<p style="color: #777777">' + pub_author + '</p>';
			}
			txt += '<p>' + pub_desc + '</p><br/>';
		}
	}	
	
	//page footer
	txt += '<p><a href="#" onclick="javascript:window.location.reload();">Return to list</a></p>';
	document.getElementById('area').innerHTML = txt;
}


function getTopicXML(theTopic) {
	// returns a table of articles based on theTopic variable
	articles = xmlrequest.responseXML.getElementsByTagName("article");
	
	//page heading
	txt = "<link href='../Styles/site.css' rel='stylesheet' type='text/css'>";
	txt += "<h1>Articles For " + theTopic + "</h1><br/>";
	txt += '<p><a href="#" onclick="javascript:window.location.reload();">Return to list</a></p><br/>';
	
	
	for (a = 0; a < articles.length; a++)
	{
		pub_name = articles[a].getElementsByTagName('name')[0].firstChild.data
		pub_desc = articles[a].getElementsByTagName('description')[0].firstChild.data
		pub_year = articles[a].getElementsByTagName('year')[0].firstChild.data
		pub_mth = articles[a].getElementsByTagName('month')[0].firstChild.data
		pub_url = articles[a].getElementsByTagName('pub_url')[0].firstChild.data
		
		//collections of topics and authors
		authors = articles[a].getElementsByTagName("author");
		topics = articles[a].getElementsByTagName("topic");
		
		containsTopic = false;
		for (t=0; t < topics.length; t++) {
			pub_topic = topics[t].firstChild.data;
			if (pub_topic == theTopic) {
				containsTopic = true;
			}
		}
		
		//filter by date and build page
		if (containsTopic) {
			txt += '<h3><a href="../resources/articles/'+ pub_url +'" target="_blank">' + pub_name + '</a></h3>';
			txt += '<p style="color: #777777">' + pub_mth + '/' + pub_year + '</p>';
			for (b=0; b < authors.length; b++) {
				pub_author = authors[b].firstChild.data;
				txt += '<p style="color: #777777">' + pub_author + '</p>';
			}
			txt += '<p>' + pub_desc + '</p><br/>';
		}
	}	
	
	//page footer
	txt += '<p><a href="#" onclick="javascript:window.location.reload();">Return to list</a></p>';
	document.getElementById('area').innerHTML = txt;
}






