/*  publicationBasics.html

Javascript functions dealing with publications
*/

// Menu of all UM publication types, author list and shopping cart - goes on all publication pages
function publicationMenu()
{
	document.write("<table align='center' border='0'><tr><td align='center'>");
		document.write(" <a href='" + top.UM_CONTRIB + "'>UM&nbsp;Contributions</a> ");
		document.write(" &nbsp; <a href='" + top.UM_PAPERS + "'>UM&nbsp;Papers</a> ");
		document.write(" &nbsp; <a href='" + top.UM_MISC_PAPERS + "'>UM&nbsp;Misc.&nbsp;Publications</a> ");
		document.write(" &nbsp; <a href='" + top.PUB_AUTHOR + "'>Author&nbsp;List</a> ");
	document.write("</td></tr></table><BR>");
}

// Get the price for a given publication
function findPrice(publication, i, number)
{
	if(publication[i]['price'] != "more")
	{
		document.write("</td><td align=center>$" + publication[i]['price'] + "</td></tr>");
		price = parseFloat(publication[i]['price']);
	}
	else
	{
		var toAdd = new Array(".1", ".2", ".3", ".4", ".5", ".6", ".7", ".8", ".9");
		j = i + toAdd[number];
		number++;
		document.write("; " + publication[j]['title']);
		findPrice(publication, i, number);
	}
	return price;
}

/* PUBLICATIONS */
// Loop to write out all UM publications
function writeUMpubs(publication)
{
	document.write("<tr><td><b>Order No.</td align=center><td><b>Description</td><td><b>Price</td>");
	var i;
	var oldI = 0;
	for(i in publication)
	{
		if(String(i) > String(oldI))	// if the new (i) is less than the old (oldI) don't write anything - already written as part of a multiple-piece publication
		{
			oldI = writePubs(publication, i);
		}
	}
}

// Write out Publications -- checkbox, volume, number, year
function writePubs(publication, i)
{
	if((publication[i]["discontinued"] == "FALSE") && (publication[i]["price"] != ""))
	{
		document.write("<tr><td valign=top width=20>" + publication[i]['ID'] + "</td><td>");			// Write out order ID so people can correctly fill out the order form
		i = writeCitation2(publication, i);
	}
	if((publication[i]["discontinued"] == "TRUE") && (publication[i]["author"]["author1"] != null))
	{
		document.write("<tr><td valign=top width=20></td><td>");			// Not available for ordering
		i = writeCitation2(publication, i);
	}
	return i;
}

// Write out second half of citation
function writeCitation2(publication, i)
{
	if(publication[i]["volume"] != "")
	{
		document.write("Vol. " + publication[i]["volume"] + "&nbsp; ");
	}
	if(publication[i]["number"] != "")
	{
		document.write("No. " + publication[i]["number"] + "&nbsp; ");
	}
	document.write(publication[i]["year"] + "<BR>");
	i = writePubCitation(publication, i);

	return i;
}

// Write out Publications -- authors, title, pages, pdf, price
function writePubCitation(publication, i)
{
	pubAuthor(publication, i);
	document.write(" &nbsp;<b>'" + publication[i]["title"] + "'</b>");
	if(publication[i]["pages"] != "")
	{
		document.write(", p. " + publication[i]["pages"]);
	}
	if(publication[i]["pdf"] != "")
	{
		document.write(" &nbsp;&nbsp;<a href='" + publication[i]["pdf"] + "'>Download PDF</a>");
	}
	if(publication[i]["price"] != "more")
	{
		if((publication[i]["discontinued"] == "FALSE") && (publication[i]["price"] != ""))
		{
			document.write("</td><td valign=bottom>$" + publication[i]["price"] + "</td>");
		}
		if((publication[i]["discontinued"] == "TRUE") && (publication[i]["author"]["author1"] != null))
		{
			document.write("</td><td valign=bottom><i>Out of print</i></td>");
		}
	}
	else
	{
		i = multiplePapers(i, publication);
	}
	return i;
}

// Write out 2nd, 3rd etc papers that are sold under a single order number
function multiplePapers(i, publication)
{
	document.write("<BR>");
	var nextIchar = i.charAt(0);
	var nextIno = i.substr(1);
	nextIno = Number(nextIno) + 1;
	var j = nextIchar+nextIno;
	j = writePubCitation(publication, j);
	return j;
}

// Compile all authors for a paper (from the multidimensional array)
function pubAuthor(publication, i)
{
	var j;
	for(j in publication[i]["author"])
	{
		if(j != "author1")
		{
			document.write("; ");
		}
		document.write(publication[i]["author"][j]);
	}
}

// Add each author to the end of an array
function fillArray(publication, authorArray, i)
{
	var foo = "";
	var bar = "";
	var test = "";
	var check = false;

	for(foo in publication)
	{
		for(bar in publication[foo]["author"])
		{
			for(test in authorArray)
			{
				if(authorArray[test] == publication[foo]["author"][bar])
				{
					check = true;
				}
			}
			if(check != true)
			{
				authorArray[i] = publication[foo]["author"][bar];
				i = i + 1;
			}
			check = false;
		}
	}
	return authorArray;
}

// Create an array of all authors from all three publications
function authorToArray()
{
	var i = 0;
	var authorArray = new Array();
	
	authorArray = fillArray(top.UMcontrib, authorArray, i);
	i = authorArray.last;
	authorArray = fillArray(top.UMmiscPapers, authorArray, i);
	
	return authorArray;
}

// Write list of all authors 
function authorPubs()
{
	var authorArray = authorToArray();
	authorArray.sort(sortDownSingle);
	var i;
	var number = authorArray.length/3;
	document.write("<table border=0 align=center>");
	for (i=1; i<authorArray.length; i++)
	{
		document.write("<tr>");
		if(authorArray[i] != undefined)
		{
			document.write("<td width=160><a href='" + top.AUTHOR_PUBS + authorArray[i] + "'>" + authorArray[i] + "</a></td>");
			i++;
		}
		if(authorArray[i] != undefined)
		{
			document.write("<td width=160><a href='" + top.AUTHOR_PUBS + authorArray[i] + "'>" + authorArray[i] + "</a></td>");
			i++;
		}
		if(authorArray[i] != undefined)
		{
			document.write("<td width=160><a href='" + top.AUTHOR_PUBS + authorArray[i] + "'>" + authorArray[i] + "</a></td>");
		}
		document.write("</tr>");
	}
	document.write("</table>");
}

// Write out all papers/contributions for a specific publication
function pubsForAuthor(publication, title)
{
	var i;
	var j;
	var alreadyUsed = 0;
	var first=1;

	for(i in publication)
	{
		if(i != alreadyUsed)
		{
			for(j in publication[i]["author"])
			{
				if(name == publication[i]["author"][j])
				{
					if(first == 1)
					{
						document.write("<tr><td align=center colspan=3 class='red'><BR><font size=+1><b><i>" + title + "</i></b></font><BR></td></tr>");
						document.write("<tr><td font-size=-1><b>Order No.</td><td font-size=-1><b>Description</td><td size=-1><b>Price</td>");
						first = 0;
					}
					alreadyUsed = writePubs(publication, i);
				}
			}
		}
	}
}

// Write out all papers for each type of publication
function listAnAuthor(name)
{
	var title = new Array();
	title[0] = "Contributions from the Museum of Paleontology";
	title[1] = "Museum of Paleontology Papers";
	title[2] = "Museum of Paleontology Miscellaneous Publications";

	pubsForAuthor(top.UMcontrib, title[0]);
	pubsForAuthor(top.UMpapers, title[1]);
	pubsForAuthor(top.UMmiscPapers, title[2]);
}

// Instructions on ordering - goes at the top of each list of publications
function pubInstructions(title)
{
	document.write(title + " can be ordered from the Publications Secretary, Museum of Paleontology, The University of Michigan, Ann Arbor, Michigan 48109-1079 U.S.A.");  
	document.write("(email: <a href='mailto:" + top.paleoPubs + "'>" + top.paleoPubs + "</a>)");
	document.write(" <BR><BR><a href='" + top.PUB_ORDERING + "'>Ordering Instructions</a>");
	document.write("<BR><BR>");
	document.write("You may view papers by chronological order (below) or browse a list of <a href='" + top.PUB_AUTHOR + "'>author's last names</a>.<BR>");
}
/* PUBLICATIONS */

