Array.prototype.in_Array = function(search_term) {
  var i = this.length;
  if (i > 0) {
	 do {
		if (this[i] === search_term) {
		   return true;
		}
	 } while (i--);
  }
  return false;
}
Array.prototype.authoredBy = Array.prototype.in_Array;

/*
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}     
*/

function isArray() {
	if (typeof arguments[0] == 'object') {  
		var criterion = arguments[0].constructor.toString().match(/array/i);
		 return (criterion != null);  
	}
	return false;
}



function loadXML()
{
	/* Test browser specific XML DOM implemenations */ 
	if (document.implementation && document.implementation.createDocument) /* Mozilla */
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = initPubs;
	}
	else if (window.ActiveXObject) /* IE */
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) initPubs();
		};
 	}
	else
	{
		alert('Browser does not support XML loading');
		return;
	}
	xmlDoc.load("publications.xml");
}

function initPubs () {
	var jsonStr = xml2json(xmlDoc.documentElement).replace('undefined',"");
	eval('pubs=' + jsonStr);
	showAuthors();
	showPubs(undefined);
}

function showAuthors () {
	var my_authors = pubs.publications.authors.author;
	var selectHTML;	
	if(isArray(my_authors)) {
		selectHTML = "<select id='authors' name='authors' onChange='showAuthorPubs()'>";
		selectHTML += "<option value='all'>All</option>:";
		for (var i = 0; i < my_authors.length; i++) {
			selectHTML += "<option value='" + my_authors[i].id + "' >" + my_authors[i].name + "</option>\n";
		}
		selectHTML += "</select>\n";
	}
	var selectBox = document.getElementById("selectbox");
	selectBox.innerHTML = selectHTML;
} 

function showAuthorPubs() {
	var authorSelect = document.getElementById('authors');
	if(!authorSelect.selectedIndex) {
		showPubs(undefined);
	} else {
		showPubs(authorSelect.options[authorSelect.selectedIndex].value);
	}
}

function showPubs(for_author) {
	
	var my_papers = pubs.publications.papers.paper;
	var paperHTML;
	if(isArray(my_papers)) {
		paperHTML = "<ol>\n"; 		
		for (var i = 0; i < my_papers.length; i++) {
			if(for_author == undefined) {
				paperHTML += "<li>" + my_papers[i].citation
				if(my_papers[i].link != undefined) {
					paperHTML += "<br /><a " + my_papers[i].link + ">" + 
					my_papers[i].linktext + "</a>"
				}
			} else {
				if((isArray(my_papers[i].author) && my_papers[i].author.authoredBy(for_author)) ||  
					(my_papers[i].author == for_author)) {
					paperHTML += "<li>" + my_papers[i].citation
					if(my_papers[i].link != undefined) {
						paperHTML += "<br /><a " + my_papers[i].link + ">" + 
						my_papers[i].linktext + "</a>"
					}
				} 
			}
			paperHTML += "</li>\n";
		}
		paperHTML += "</ol>\n";
	}

	var paperBox = document.getElementById("papersbox");	
	paperBox.innerHTML = paperHTML;

}

