//pluralizes node values that appear in multiples
function pluralizeWord(nodeList,nodeValue)
{
	var nodeCount=0;
	var vowelArray=new Array("a","e","i","o","u");
	var precedingVowel=false;

	for (j=0;j<nodeList.length;j++)
	{
		if (nodeList[j].firstChild.nodeValue==nodeValue) nodeCount++;
	}

	if (nodeCount>1) 
	{
		//handle the special pluralization of Editor in Chief
		if (nodeValue=="Editor in Chief") nodeValue="Editors in Chief";
		//handle all other cases
		else if (nodeValue.charAt(nodeValue.length-1)=="y")
		{
			for (k=0;k<vowelArray.length;k++)
			{
				if (nodeValue.charAt(nodeValue.length-2)!=vowelArray[k]) precedingVowel=true;
			}
			if (!precedingVowel) nodeValue=nodeValue.substring(0,nodeValue.length-2)+"ies";
			else nodeValue+="s";
		}
		else nodeValue+="s";
	}

	return nodeValue;
}

//assembles html 
function displayText()
{
	var editionList=null;
	var queryString="";
	var	volumeNum=0;
  	var issueNum=0;
	var volume=0;
    var volumeNoZeros=0;
	var issue=0;
	var editorList=null;
	var roleList=null;
	var currentRole="";
	var editorRole="";
	var editorName="";
	var articleType="";
	var	fileName="";
	var	title="";
	var articleList=null;
	var articleTypeList=null;
	var currentArticleType="";
	var articleTypePlural="";
	var authorName="";
	var reviewer="";
	var translator="";

	//check ready state
	if(xmlhttp.readyState!=4)
	{
		return;
	}

	if(xmlhttp.status!=200)
	{
	  	alert("Problem retrieving XML data.");
	  	return;
	}

//get volume and issue from querystring or set to latest version if no querystring
	editionList=xmlhttp.responseXML.documentElement.getElementsByTagName("EDITION");

	//get the value from the querystring 
	queryString=window.location.search;
    //alert(queryString);
	if (queryString.substring(0,1)=='?')
	{ 
		queryString=queryString.substring(1);
		queryData=queryString.split(','); 
		volume=queryData[0];
		issue=queryData[1];
	}
	//otherwise set the volume and issue to the latest version
	else 
	{
		for (i=0;i<editionList.length;i++) 
		{
			volumeNum=editionList[i].getAttribute("volume");
		  	issueNum=editionList[i].getAttribute("issue");

			if (volumeNum>=volume) 
			{
				volume=volumeNum;
				issue=issueNum;
			}
			else if (volumeNum==volume && issueNum>issueNum) 
			{
				issue=issueNum;
			}
		}
	}

//initialize text strings
	editortxt="<h1>Editorial Staff</h1><hr>";
	articletxt="<h1>Volume " + volume + ", Issue " + issue + "</h1><hr><ul>";

//build editor list string
	for (x=0;x<editionList.length;x++) 
	{
		volumeNum=editionList[x].getAttribute("volume");
	  	issueNum=editionList[x].getAttribute("issue");
	  	
		if (volumeNum==volume && issueNum==issue)
    	{ 
			  	editorList=editionList[x].getElementsByTagName("EDITOR");
				if (editorList.length>0)
				{
				  	roleList=editionList[x].getElementsByTagName("ROLE");
	
				  	for (y=0;y<editorList.length;y++)
					{
				    	editorRole=editorList[y].getElementsByTagName("ROLE")[0].firstChild.nodeValue;
				    	editorName=editorList[y].getElementsByTagName("NAME")[0].firstChild.nodeValue;
		
						if (editorRole!=currentRole)
						{
			        		currentRole=editorRole;
							editorRole=pluralizeWord(roleList,editorRole);
							editortxt+="<h3>" + editorRole + "</h3><p class='name'>" + editorName + "</p>";
			        	}
						else
						{
			        		editortxt+="<p class='name'>" + editorName + "</p>";
			        	}
					}
				}
				else editortxt="";

//build article list string
			articleList=editionList[x].getElementsByTagName("ARTICLE");
			articleTypeList=editionList[x].getElementsByTagName("TYPE");

			currentArticleType="";
            if (volume<10)
			{
                volumeNoZeros=volume*1;
                articletxt="<h1>Volume " + volumeNoZeros + ", Issue " + issue + "</h1><hr>";
            }
            else
            {
                articletxt="<h1>Volume " + volume + ", Issue " + issue + "</h1><hr>";
            }
            

			for (z=0;z<articleList.length;z++)
			{
				try
				{
					articleType=articleList[z].getElementsByTagName("TYPE")[0].firstChild.nodeValue;
					fileName=articleList[z].getElementsByTagName("FILENAME")[0].firstChild.nodeValue;
					title=articleList[z].getElementsByTagName("TITLE")[0].firstChild.nodeValue;
				}
				catch (e) {	}

				if (articleType!=currentArticleType && articleType!="Editor")
				{
					currentArticleType=articleType;
					articleTypePlural=pluralizeWord(articleTypeList,articleType);
					articletxt+="</ul><h2>" + articleTypePlural + "</h2><ul>";
				}

				switch (articleType)
				{
					case "Editor":
				      	articletxt+="<ul><li><a href='articles/" + fileName + "' class='black' target='_blank'><i>" + title + "</i></a></li></ul><ul>";
						break;
 					case "Translation":
						try
						{
							authorName="";
							authorName=articleList[z].getElementsByTagName("AUTHOR")[0].firstChild.nodeValue;
							authorName+=", ";
						}
						catch (e)
						{
							authorName+="";
						}
						translator=articleList[z].getElementsByTagName("TRANSLATOR")[0].firstChild.nodeValue;
			      		articletxt+="<li><a href='articles/" + fileName + "' class='black' target='_blank'>" + authorName + "<i>" + title + "</i>, translation by " + translator + "</a></li>";
						break;
					default:
						authorName=articleList[z].getElementsByTagName("AUTHOR")[0].firstChild.nodeValue;
				      	articletxt+="<li><a href='articles/" + fileName + "' class='black' target='_blank'>" + authorName + ", <i>" + title + "</i></a></li>";
				}
			}
			articletxt+="</ul>";
		}
	}
//write the assembled text strings
document.getElementById('display_editors').innerHTML=editortxt;
document.getElementById('display_articles').innerHTML=articletxt;
}
