/* Acronomicon Javascript
 * Written by Ben O'Donnell, 2007
 */

var curPrimaryList = null;
var curSecondaryList = null;
var primaryUpdate = null;
var secondaryUpdate = null;

function infoViewer( reqNum , reqType , reqName , reqLinkFunctionName , reqArgs , reqTitle , reqFormat )
{
	this.number = reqNum;
	this.name = reqName;
	this.type = reqType;
	this.linkFunctionName = reqLinkFunctionName;
	this.args = reqArgs;
	this.title = reqTitle;
	this.format = reqFormat;
	this.page = 0;
	this.limit = 4;
	this.numresults = 16;
	switch (reqFormat)
	{
		case "primary-comments-view":
		case "primary-votes-view":
		case "primary-my-nomes-view":
		case "secondary-nome-list":
		case "secondary-comment-list":
			this.limit = 10;
	}
	this.pageArgs = "&page="+this.page+"&limit="+this.limit;
	if (this.args == "")
	{
		this.pageArgs = "?page="+this.page+"&limit="+this.limit;
	}
	
	this.nextPage = function()
	{
		if (this.numresults < this.limit)
		{
			return;
		}
		this.page += this.limit;
		if (this.args == "")
		{
			this.pageArgs = "?page="+this.page+"&limit="+this.limit;
		}
		else
		{
			this.pageArgs = "&page="+this.page+"&limit="+this.limit;
		}
		this.refresh();
	}
	
	this.prevPage = function()
	{
		if (this.page == 0)
		{
			return;
		}
		this.page -= this.limit;
		if (this.page < 0)
		{
			this.page = 0;
		}
		if (this.args == "")
		{
			this.pageArgs = "?page="+this.page+"&limit="+this.limit;
		}
		else
		{
			this.pageArgs = "&page="+this.page+"&limit="+this.limit;
		}
		this.refresh();
	}

	this.refresh = function()
	{
		switch( this.type )
		{
			case "nym":
				getAcronyms( this.name , this.linkFunctionName , this.args + this.pageArgs , this.title , this.format );
				break;
			case "nome":
				getAcronomes( this.name , this.linkFunctionName , this.args + this.pageArgs , this.title , this.format );
				break;
			case "comment":
				getComments( this.name , this.linkFunctionName , this.args + this.pageArgs , this.title , this.format );
				break;
			case "vote":
				getVotes( this.name , this.linkFunctionName , this.args + this.pageArgs , this.title , this.format );
				break;
			case "data":
				getData( this.name , this.linkFunctionName , this.args + this.pageArgs , this.title , this.format );
				break;
		}
		if (this.number > 0)
		{
			document.getElementById(this.name + "-selector").innerHTML = this.makeSelectorHTML();
		}
		else
		{
			//generate sort options for main views.
		}
	}
	
	this.refreshVote = function()
	{
		switch ( this.type )
		{
			case "nym":
				break;
			case "nome":
				switch ( this.format )
				{
					case "viewer-top-rated":
					case "primary-nome-view":
					this.refresh();
				}
				break;
			case "comment":
				break;
			case "vote":
				break;
		}
	}
	
	this.makeSelectorHTML = function()
	{
		var selectorString = '<ul class="drop-down-menu">';
		selectorString += '<li class="drop-down-nyms">Nyms<ul>';
		selectorString += '<li><a href="javascript:viewers[' + this.number + '] = new infoViewer(' + this.number + ',' + "'nym','" + this.name + "', 'showAcronymById', '', 'New Nyms', 'viewer-recent');viewers[" + this.number + '].refresh()">New</a></li>';
		selectorString += '<li><a href="javascript:viewers[' + this.number + '] = new infoViewer(' + this.number + ',' + "'nym','" + this.name + "', 'showAcronymById', '?nyms-sort=2', 'Most Popular Nyms', 'viewer-popular-nyms');viewers[" + this.number + '].refresh()">Popular</a></li>';
		selectorString += '</ul></li>';
		selectorString += '<li class="drop-down-nomes">Nomes<ul>';
		selectorString += '<li><a href="javascript:viewers[' + this.number + '] = new infoViewer(' + this.number + ',' + "'nome','" + this.name + "', 'showAcronomeById', '', 'New Nomes', 'viewer-recent');viewers[" + this.number + '].refresh()">New</a></li>';
		selectorString += '<li><a href="javascript:viewers[' + this.number + '] = new infoViewer(' + this.number + ',' + "'nome','" + this.name + "', 'showAcronomeById', '?nomes-sort=4', 'Top Rated Nomes', 'viewer-top-rated');viewers[" + this.number + '].refresh()">Top Rated</a></li>';
		selectorString += '</ul></li></ul>'
		return selectorString;
	}
}

function getXmlHttpRequest()
{
	var xmlHttp = null;
	try {
		//for Opera 8.0+, Firefox, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch(e) {
		//this should work for IE
		try {
			//for IE 6.0+
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				//for IE 5.5+
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser doesn't seem to allow for ajax style scripting");
				return false;
			}
		}
	}
	return xmlHttp;
}
 
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function fetchPhpVars()
{
	hideElement("user-info");
	userId = document.getElementById("user-id").innerHTML;
	userName = document.getElementById("user-name").innerHTML;
}

function getAcronyms( outputId , linkFunctionName , getArgs , listTitle , listFormat )
{
	var viewerRef = getViewerRef(outputId);
	var outputTitleId = outputId+"-title";
	var outputContentId = outputId+"-content";
	var GetAcronymsReq = getXmlHttpRequest();
	GetAcronymsReq.onreadystatechange = function()
	{
		if(GetAcronymsReq.readyState == 4)
		{
			if(GetAcronymsReq.status == 200)
			{
				document.getElementById(outputTitleId).innerHTML = listTitle;
				document.getElementById(outputContentId).innerHTML = "done.";
				var NymsList = GetAcronymsReq.responseXML.documentElement;
				if (viewerRef != null)
				{
					viewerRef.numresults = NymsList.getElementsByTagName("numresults")[0].firstChild.data;
				}
				var formattedNymsList = "";
				switch (listFormat)
				{
					case "viewer-recent":
					case "viewer-popular-nyms": //for now this uses the same format as the recent nyms viewer.  Perhaps it will get its own formatting function at some juncture.
						formattedNymsList = formatRecentNymsListViewer( NymsList , linkFunctionName );
						break;
					case "primary-nym-view":
						formattedNymsList = formatPrimaryNymView( NymsList , linkFunctionName );
						break;
					case "compose-nome-view":
						formattedNymsList = formatComposeNymView( NymsList , linkFunctionName );
						break;
				}
				document.getElementById(outputContentId).innerHTML = formattedNymsList;
				switch (outputId)
				{
					case "primary-viewer":
						curPrimaryList = NymsList;
						break;
				}
			}
			else
			{
				document.getElementById(outputContentId).innerHTML = "Hey joe, there's been a connection error of some kind.";
			}
		}
		else
		{
			document.getElementById(outputContentId).innerHTML = "Loading...";
		}
	}
	var GetAcronymsURL = "get_nyms.php" + getArgs;
	GetAcronymsReq.open("GET", GetAcronymsURL, true);
	GetAcronymsReq.send(null);
}

function formatRecentNymsListViewer( nymsXMLRecord , linkFunctionName )
{
	var formattedString = '<ul class="recent-acronyms-list">';
	if (nymsXMLRecord.getElementsByTagName("numresults")[0].firstChild.data == 0)
	{
		formattedString += '<li>No Nyms left to display.</li>';
	}
	else
	{
		for (i=1; i<nymsXMLRecord.childNodes.length; i++)
		{
			formattedString += '<li class="recent-acronym">';
			formattedString += '<a href="javascript:' + linkFunctionName + '(' + nymsXMLRecord.childNodes[i].getElementsByTagName("id")[0].firstChild.data + ')"><span class="nym">';
			formattedString += nymsXMLRecord.childNodes[i].getElementsByTagName("text")[0].firstChild.data;
			formattedString += '</span><br /><span class="num-nomes">' + nymsXMLRecord.childNodes[i].getElementsByTagName("num-nomes")[0].firstChild.data + '</span> nome';
			if (nymsXMLRecord.childNodes[i].getElementsByTagName("num-nomes")[0].firstChild.data != 1)
			{
				formattedString += "s"; //for proper plurals.
			}
			formattedString += ' submitted';
			formattedString += '<br />since: <span class="date">' + formatDate(nymsXMLRecord.childNodes[i].getElementsByTagName("date")[0].firstChild.data) + '</span>';
			formattedString += '</a></li>';
		}
	}
	formattedString += '</ul>';
	return formattedString;
}

function formatPrimaryNymView( nymsXMLRecord , linkFunctionName )
{
	formattedString = '<span class="nym">';
	formattedString += nymsXMLRecord.childNodes[1].getElementsByTagName("text")[0].firstChild.data;
	formattedString += '</span>';
	formattedString += '<br /><span class="num-nomes">' + nymsXMLRecord.childNodes[1].getElementsByTagName("num-nomes")[0].firstChild.data + '</span> submitted for this nym.';
	formattedString += '<br /><span class="date">' + formatDate(nymsXMLRecord.childNodes[1].getElementsByTagName("date")[0].firstChild.data) + '</span>';
	formattedString += '<br /><span class="composer-link"><a href="acro_compose.php?nymid=' + nymsXMLRecord.childNodes[1].getElementsByTagName("id")[0].firstChild.data + '">I have a great nome for this nym...</a></span>';
	return formattedString;
}

function formatComposeNymView( NymsList , linkFunctionName )
{
	formattedString = '<span class="nym">';
	formattedString += NymsList.childNodes[1].getElementsByTagName("text")[0].firstChild.data;
	formattedString += '</span>';
	formattedString += '<br /><span class="num-nomes">' + NymsList.childNodes[1].getElementsByTagName("num-nomes")[0].firstChild.data + '</span> Nome';
	if (NymsList.childNodes[1].getElementsByTagName("num-nomes")[0].firstChild.data != 1)
	{
		formattedString += 's';
	}
	formattedString += ' submitted for this nym since ';
	formattedString += '<span class="date">' + formatDate(NymsList.childNodes[1].getElementsByTagName("date")[0].firstChild.data) + '</span>';
	return formattedString;
}

function getAcronomes( outputId , linkFunctionName , getArgs , listTitle , listFormat )
{
	var viewerRef = getViewerRef(outputId);
	var outputTitleId = outputId+"-title";
	var outputContentId = outputId+"-content";
	var GetAcronomesReq = getXmlHttpRequest();
	GetAcronomesReq.onreadystatechange = function()
	{
		if(GetAcronomesReq.readyState == 4)
		{
			if(GetAcronomesReq.status == 200)
			{
				document.getElementById(outputTitleId).innerHTML = listTitle;
				document.getElementById(outputContentId).innerHTML = "done.";
				var NomesList = GetAcronomesReq.responseXML.documentElement;
				if (viewerRef != null)
				{
					viewerRef.numresults = NomesList.getElementsByTagName("numresults")[0].firstChild.data;
				}
				var formattedNomesList = "";
				switch ( listFormat )
				{
					case "viewer-recent":
						formattedNomesList = formatRecentNomesListViewer( NomesList , linkFunctionName );
						break;
					case "viewer-top-rated":
						formattedNomesList = formatTopRatedNomesListViewer( NomesList , linkFunctionName );
						break;
					case "primary-nome-view":
						formattedNomesList = formatPrimaryNomeView( NomesList , linkFunctionName );
						break;
					case "secondary-nome-list":
						formattedNomesList = formatSecondaryNomeView( NomesList , linkFunctionName );
						break;
					case "primary-my-nomes-view":
						formattedNomesList = formatPrimaryMyNomesView( NomesList , linkFunctionName );
						break;
				}
				document.getElementById(outputContentId).innerHTML = formattedNomesList;
				switch (outputId)
				{
					case "primary-viewer":
						if (NomesList.childNodes[1].getElementsByTagName("author-id")[0].firstChild.data == userId)
						{
							hideElement("vote-box");
						}
						break;
				}
			}
			else
			{
				document.getElementById(outputContentId).innerHTML = "Hey joe, there's been a connection error of some kind.";
			}
		}
		else
		{
			document.getElementById(outputContentId).innerHTML = "Loading...";
		}
	}
	var GetAcronomesURL = "get_nomes.php" + getArgs;
	GetAcronomesReq.open("GET", GetAcronomesURL, true);
	GetAcronomesReq.send(null);
}

function formatRecentNomesListViewer( nomesXMLRecord , linkFunctionName )
{
	var formattedString = '<ul class="recent-nomes-list">';
	if (nomesXMLRecord.getElementsByTagName("numresults")[0].firstChild.data == 0)
	{
		formattedString += '<li>No Nomes to Display.</li>';
	}
	else
	{
		for (i=1; i<nomesXMLRecord.childNodes.length; i++)
		{
			formattedString += '<li class="recent-nome">';
			formattedString += '<a href="javascript:' + linkFunctionName + '(' + nomesXMLRecord.childNodes[i].getElementsByTagName("id")[0].firstChild.data + ')"><span class="nome">';
			formattedString += nomesXMLRecord.childNodes[i].getElementsByTagName("text")[0].firstChild.data;
			formattedString += '</span><br />writ by <span class="author">' + nomesXMLRecord.childNodes[i].getElementsByTagName("author")[0].firstChild.data + '</span>';
			formattedString += ' at <span class="date">' + formatDate(nomesXMLRecord.childNodes[i].getElementsByTagName("date")[0].firstChild.data) + '</span>';
			formattedString += '</a></li>';
		}
	}
	formattedString += '</ul>'
	return formattedString;
}

function formatTopRatedNomesListViewer( nomesXMLRecord , linkFunctionName )
{
	var formattedString = '<ul class="top-rated-nomes-list">';
	if (nomesXMLRecord.getElementsByTagName("numresults")[0].firstChild.data == 0)
	{
		formattedString += '<li>No Nomes to display.</li>';
	}
	else
	{
		for (i=1; i<nomesXMLRecord.childNodes.length; i++)
		{
			formattedString += '<li class="top-rated-nome">';
			formattedString += '<a href="javascript:' + linkFunctionName + '(' + nomesXMLRecord.childNodes[i].getElementsByTagName("id")[0].firstChild.data + ')"><span class="nome">';
			formattedString += nomesXMLRecord.childNodes[i].getElementsByTagName("text")[0].firstChild.data;
			formattedString += '</span><br />';
			formattedString += '<span class="rating">';
			if (nomesXMLRecord.childNodes[i].getElementsByTagName("rating")[0].firstChild == null)
			{
				formattedString += "Not Rated";
			}
			else
			{
				formattedString += formatRating( nomesXMLRecord.childNodes[i].getElementsByTagName("rating")[0].firstChild.data, "small");
			}
			formattedString += '</span>';
			formattedString += '</a></li>';
		}
	}
	formattedString += '</ul>'
	return formattedString;
}

function formatPrimaryNomeView( nomesXMLRecord , linkFunctionName )
{
	var formattedString = '<span class="nome"><a href="javascript:showAcronymById(' + nomesXMLRecord.childNodes[1].getElementsByTagName("nym-id")[0].firstChild.data + ')">' + nomesXMLRecord.childNodes[1].getElementsByTagName("text")[0].firstChild.data + '</a></span>';
	formattedString += '<br /><span class="author">' + nomesXMLRecord.childNodes[1].getElementsByTagName("author")[0].firstChild.data + '</span>';
	if (nomesXMLRecord.childNodes[1].getElementsByTagName("rating")[0].firstChild == null)
	{
		formattedString += '<span class="rating">Not Rated</span>';
	}
	else
	{
		formattedString += 'Rated <span class="rating">' + formatRating(nomesXMLRecord.childNodes[1].getElementsByTagName("rating")[0].firstChild.data, "") + '</span> by <span class="num-votes">' + nomesXMLRecord.childNodes[1].getElementsByTagName("num-votes")[0].firstChild.data + ' users.</span>';
	}
	formattedString += '<br /><span class="date">' + formatDate(nomesXMLRecord.childNodes[1].getElementsByTagName("date")[0].firstChild.data) + '</span>';
	return formattedString;
}

function formatSecondaryNomeView( nomesXMLRecord , linkFunctionName )
{
	var formattedString = '<ul class="nym-nomes-list">';
	if (nomesXMLRecord.getElementsByTagName("numresults")[0].firstChild.data == 0)
	{
		formattedString += '<li class="no-data">No nomes submitted for this nym.</li>';
	}
	else
	{
		for (i=1; i<nomesXMLRecord.childNodes.length; i++)
		{
			formattedString += '<li class="nym-nome">';
			formattedString += '<a href="javascript:' + linkFunctionName + '(' + nomesXMLRecord.childNodes[i].getElementsByTagName("id")[0].firstChild.data + ')"><span class="nome">';
			formattedString += nomesXMLRecord.childNodes[i].getElementsByTagName("text")[0].firstChild.data;
			formattedString += '</span></a><br /><span class="author">' + nomesXMLRecord.childNodes[i].getElementsByTagName("author")[0].firstChild.data + '</span>';
			formattedString += '<span class="rating">';
			if (nomesXMLRecord.childNodes[i].getElementsByTagName("rating")[0].firstChild == null)
			{
				formattedString += "Not Rated";
			}
			else
			{
				formattedString += formatRating(nomesXMLRecord.childNodes[i].getElementsByTagName("rating")[0].firstChild.data, "small");
			}
			formattedString += '</span>';
			formattedString += '<span class="date">' + formatDate(nomesXMLRecord.childNodes[i].getElementsByTagName("date")[0].firstChild.data) + '</span>';
			formattedString += '</li>';
		}
	}
	formattedString += '</ul>';
	return formattedString;
}

function formatPrimaryMyNomesView( nomesXMLRecord , linkFunctionName )
{
	var formattedString = '<ul id="my-nomes-list">';
	if (nomesXMLRecord.getElementsByTagName("numresults")[0].firstChild.data == 0)
	{
		formattedString += '<li class="no-data">No nomes to display.</li>';
	}
	else
	{
		for (i=1; i<nomesXMLRecord.childNodes.length; i++)
		{
			formattedString += '<li class="my-nome">';
			formattedString += '<a href="javascript:' + linkFunctionName + '(' + nomesXMLRecord.childNodes[i].getElementsByTagName("id")[0].firstChild.data + ')"><span class="nome">';
			formattedString += nomesXMLRecord.childNodes[i].getElementsByTagName("text")[0].firstChild.data;
			formattedString += '</span></a><br /><span class="rating">';
			if (nomesXMLRecord.childNodes[i].getElementsByTagName("rating")[0].firstChild == null)
			{
				formattedString += "Not Rated";
			}
			else
			{
				formattedString += formatRating(nomesXMLRecord.childNodes[i].getElementsByTagName("rating")[0].firstChild.data, "");
			}
			formattedString += '</span>';
			formattedString += '<span class="date">' + formatDate(nomesXMLRecord.childNodes[i].getElementsByTagName("date")[0].firstChild.data) + '</span>';
			formattedString += '</li>';
		}
	}
	formattedString += '</ul>';
	return formattedString;
}

function getComments( outputId , linkFunctionName , getArgs , listTitle , listFormat )
{
	var viewerRef = getViewerRef(outputId);
	var outputTitleId = outputId+"-title";
	var outputContentId = outputId+"-content";
	var GetCommentsReq = getXmlHttpRequest();
	GetCommentsReq.onreadystatechange = function()
	{
		if(GetCommentsReq.readyState == 4)
		{
			if(GetCommentsReq.status == 200)
			{
				document.getElementById(outputTitleId).innerHTML = listTitle;
				document.getElementById(outputContentId).innerHTML = "done.";
				var CommentsList = GetCommentsReq.responseXML.documentElement;
				if (viewerRef != null)
				{
					viewerRef.numresults = CommentsList.getElementsByTagName("numresults")[0].firstChild.data;
				}
				var formattedCommentsList = "";
				switch ( listFormat )
				{
					case "secondary-comment-list":
						formattedCommentsList = formatSecondaryCommentsList( CommentsList , linkFunctionName );
						break;
					case "primary-comments-view":
						formattedCommentsList = formatPrimaryMyCommentsView( CommentsList , linkFunctionName );
						break;
				}
				document.getElementById(outputContentId).innerHTML = formattedCommentsList;
			}
			else
			{
				document.getElementById(outputContentId).innerHTML = "Hey joe, there's been a connection error of some kind.";
			}
		}
		else
		{
			document.getElementById(outputContentId).innerHTML = "Loading...";
		}
	}
	var GetCommentsURL = "get_comments.php" + getArgs;
	GetCommentsReq.open("GET", GetCommentsURL, true);
	GetCommentsReq.send(null);
}

function formatSecondaryCommentsList( CommentsList , LinkFunctionName )
{
	var formattedString = '<ul class="comment-list">';
	if (CommentsList.getElementsByTagName("numresults")[0].firstChild.data == 0)
	{
		formattedString += '<li class="no-comment">no comments; perhaps you\'d like to leave the first?</li>';
	}
	else
	{
		for (i=1; i<CommentsList.childNodes.length; i++)
		{
			formattedString += '<li class="comment"><span class="comment-text">';
			formattedString += CommentsList.childNodes[i].getElementsByTagName("text")[0].firstChild.data;
			formattedString += '</span><br /><span class="author">' + CommentsList.childNodes[i].getElementsByTagName("author")[0].firstChild.data + '</span>';
			formattedString += '<span class="date">';
			formattedString += formatDate(CommentsList.childNodes[i].getElementsByTagName("date")[0].firstChild.data);
			formattedString += '</span>';
			formattedString += '</li>';
		}
	}
	formattedString += '</ul>'
	return formattedString;
}

function formatPrimaryMyCommentsView( CommentsList , linkFunctionName )
{
	var formattedString = '<ul class="comment-list">';
	if (CommentsList.getElementsByTagName("numresults")[0].firstChild.data == 0)
	{
		formattedString += '<li class="no-comment">No comments to display.</li>';
	}
	else
	{
		for (i=1; i<CommentsList.childNodes.length; i++)
		{
			formattedString += '<li class="comment">';
			formattedString += '<a href="javascript:' + linkFunctionName + '(' + CommentsList.childNodes[i].getElementsByTagName("acronome")[0].firstChild.data + ')"><span="comment-text">';
			formattedString += CommentsList.childNodes[i].getElementsByTagName("text")[0].firstChild.data;
			formattedString += '</span></a><br /><span class="author">' + CommentsList.childNodes[i].getElementsByTagName("author")[0].firstChild.data + '</span>';
			formattedString += '<span class="date">';
			formattedString += formatDate(CommentsList.childNodes[i].getElementsByTagName("date")[0].firstChild.data);
			formattedString += '</span>';
			formattedString += '</li>';
		}
	}
	formattedString += '</ul>'
	return formattedString;
}

function getVotes( outputId , linkFunctionName , getArgs , listTitle , listFormat )
{
	var viewerRef = getViewerRef(outputId);
	var outputTitleId = outputId+"-title";
	var outputContentId = outputId+"-content";
	var fetchVoteReq = getXmlHttpRequest();
	fetchVoteReq.onreadystatechange = function()
	{
		if (fetchVoteReq.readyState == 4)
		{
			if (fetchVoteReq.status == 200)
			{
				document.getElementById(outputTitleId).innerHTML = listTitle;
				document.getElementById( outputContentId ).innerHTML = "done.";
				var voteListData = fetchVoteReq.responseXML.documentElement;
				if (viewerRef != null)
				{
					viewerRef.numresults = voteListData.getElementsByTagName("numresults")[0].firstChild.data;
				}
				var formattedVoteList = "";
				switch( listFormat )
				{
					case "primary-vote-selection":
						formattedVoteList = formatPrimaryVoteSelector( voteListData , linkFunctionName );
						break;
					case "primary-votes-view":
						formattedVoteList = formatPrimaryVoteView( voteListData , linkFunctionName );
						break;
				}
				document.getElementById( outputContentId ).innerHTML = formattedVoteList;
				
			}
			else
			{
				document.getElementById( outputContentId ).innerHTML = "An html error " + fetchVoteReq.status + " has occurred.  Sorry...";
			}
		}
		else
		{
			document.getElementById( outputContentId ).innerHTML = "Loading...";
		}
	}
	fetchVoteURL = "get_vote.php" + getArgs;
	fetchVoteReq.open("GET", fetchVoteURL, true);
	fetchVoteReq.send(null);
}

function formatPrimaryVoteSelector( voteListData , linkFunctionName )
{
	//this function makes the voting box.  In this case linkFunctionName holds the nome id that is under consideration.
	var voteString = '<span id="vote info">';
	var curVote = 10;
	if (voteListData.getElementsByTagName("numresults")[0].firstChild.data == 0)
	{
		voteString += "You haven't yet voted on this Nome.  You may cast a vote at any time.";
	}
	else
	{
		curVote = voteListData.getElementsByTagName("value")[0].firstChild.data;
		voteString += "(You already voted " + formatRating(curVote, "small") + " on this Nome, but you can change your vote, if you think it'll help.)";
	}
	voteString += '</span><br />';
	voteString += '<div id="vote-options" class="vote-options-' + curVote + '">';
	for (i=0; i<7; i += 2)
	{
		if (i==curVote)
		{
			voteString += '<a class="vote-choice-' + curVote + '">&nbsp;</a>';
		}
		else
		{
			voteString += '<a href="javascript:castVote(' + "'vote-box'," + i + ',' + linkFunctionName + ')" onMouseOver="renderVote(' + i + ')" onMouseOut="renderVote(' + curVote + ')" class="vote-choice-' + i + '">&nbsp;</a>';
		}
	}
	voteString += '</div>';
	return voteString;
}

function formatPrimaryVoteView( voteListData , linkFunctionName )
{
	var voteString = '<ul id="my-votes-list">';
	if (voteListData.getElementsByTagName("numresults")[0].firstChild.data == 0)
	{
		voteString += '<li class="no-data">No votes to display.</li>';
	}
	else
	{
		for (i=1; i < voteListData.childNodes.length; i++)
		{
			voteString += '<li class="vote">';
			voteString += '<span class="value">' + formatRating(voteListData.childNodes[i].getElementsByTagName("value")[0].firstChild.data, "") + '</span>';
			voteString += '<a href="javascript:' + linkFunctionName + '(' + voteListData.childNodes[i].getElementsByTagName("acronome")[0].firstChild.data + ')"><span class="nome">' + voteListData.childNodes[i].getElementsByTagName("acronome-text")[0].firstChild.data + '</span></a>';
			voteString += '<br />Vote Cast: <span class="date">' + formatDate(voteListData.childNodes[i].getElementsByTagName("date")[0].firstChild.data) + '</span>';
			voteString += '</li>';
		}
	}
	voteString += '</ul>';
	return voteString;
}

function getData( outputId , linkFunctionName , getArgs , listTitle , listFormat )
{
	var viewerRef = getViewerRef(outputId);
	var outputTitleId = outputId+"-title";
	var outputContentId = outputId+"-content";
	var fetchDataReq = getXmlHttpRequest();
	fetchDataReq.onreadystatechange = function()
	{
		if (fetchDataReq.readyState == 4)
		{
			if (fetchDataReq.status == 200)
			{
				document.getElementById(outputTitleId).innerHTML = listTitle;
				document.getElementById( outputContentId ).innerHTML = "done.";
				var dataList = fetchDataReq.responseXML.documentElement;
				if (viewerRef != null)
				{
					viewerRef.numresults = dataList.getElementsByTagName("numresults")[0].firstChild.data;
				}
				var formattedDataList = "";
				switch( listFormat )
				{
					case "primary-news-data":
						formattedDataList = formatPrimaryNewsData( dataList , linkFunctionName );
						break;
				}
				document.getElementById( outputContentId ).innerHTML = formattedDataList;
				
			}
			else
			{
				document.getElementById( outputContentId ).innerHTML = "An html error " + fetchDataReq.status + " has occurred.  Sorry...";
			}
		}
		else
		{
			document.getElementById( outputContentId ).innerHTML = "Loading...";
		}
	}
	fetchDataURL = "get_data.php" + getArgs;
	fetchDataReq.open("GET", fetchDataURL, true);
	fetchDataReq.send(null);
}

function formatPrimaryNewsData( dataList , linkFunctionName )
{
	var formattedList = '<ul class="news-list">';
	if (dataList.getElementsByTagName("numresults")[0].firstChild.data == 0)
	{
		formattedList += '<li class="news-item">No news items.</li>';
	}
	else
	{
		for (i = 1; i < dataList.childNodes.length; i++)
		{
			formattedList += '<li class="news-item">';
			formattedList += '<span class="text">' + dataList.childNodes[i].getElementsByTagName("text")[0].firstChild.data + '</span>';
			formattedList += '<br /><span class="date">' + formatDate(dataList.childNodes[i].getElementsByTagName("date")[0].firstChild.data) + '</span>';
			formattedList += '</li>';
		}
	}
	formattedList += '</ul>';
	return formattedList;
}

function formatDate( sqldate )
{
	var jsdate = new Date();
	var curdate = new Date();
	var formattedDate = "";
	jsdate.setTime( sqldate * 1000 );
	formattedDate = jsdate.getDate() + "." + (jsdate.getMonth() + 1) + "." + jsdate.getFullYear() + "  " + jsdate.getHours() + ":" + jsdate.getMinutes();
	return formattedDate;
}

function formatRating( rating , size )
{
	rating = Math.round(rating - 0.5);
	if (!(size == ""))
	{
		size = size + '-';
	}
	var formattedRating = '<img src="images/vote-' + size + rating + '.jpg" />';
	return formattedRating;
}

function getViewerRef( outputId )
{
	switch (outputId)
	{
		case "viewer-one":
			return viewers[1];
			break;
		case "viewer-two":
			return viewers[2];
			break;
		case "viewer-three":
			return viewers[3];
			break;
		case "primary-viewer":
			return primaryViewer;
			break;
		case "secondary-viewer":
			return secondaryViewer;
			break;
	}
	return null;
}

function renderVote( vote )
{
	document.getElementById("vote-options").className = "vote-options-" + vote;
	return;
}

function toggleElementVisibility( elementId )
{
	if (document.getElementById(elementId).style.display == "none")
	{
		document.getElementById(elementId).style.display = "";
	}
	else
	{
		document.getElementById(elementId).style.display = "none";
	}
}

function showElement( elementId )
{
	document.getElementById( elementId ).style.display = "";
}

function hideElement( elementId )
{
	document.getElementById( elementId ).style.display = "none";
}
