var gSessionID="";
var gArticles=[];
var gLinkTimerID=0;
var gMode=99;
var gDisplayed=0;
var gReady=0;
var gQueryLimit=100;
var gVisitWaiting=[];
var gTopArticles;
var gFavList="";

var gLoadTop=0;
var gLoadSummary=0;
var gLoadFav=0;

if (!window.console || !console.firebug)
{
var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
window.console = {};
for (var i = 0; i < names.length; ++i)
window.console[names[i]] = function() {}
}
//try { console.assert(1); } catch(e) { console = { log: function() {},  assert: function() {} } }

function Startup(sid) {

	if (sid.length == 0) {
		console.log("sid.length=0");

		gSessionID=getCookie("sid");
		if (gSessionID==null) { gSessionID = ""; }

		if (gSessionID.length > 0) {
			// If the sid provided is empty, but we get a sid from the cookie, then 
			// it means people have gone straight to the main.html when they should 
			// have gone to app.  We should pass them on to /rhap/.
			document.location.href="/rhap/";
		}
		else {
			show("login");
		}
	}
	else {
		gSessionID = sid;
		console.log("auto-login.  sid="+sid);
	}

	var nin=getCookie("intro");
	if (nin==null || nin=="0") {
		show("intro");
		Nifty("div#intro h3", "top");
	}

	Nifty("div#article_tabs a", "small transparent top same-height");
	Nifty("ul#split h3","top");
	Nifty("div#usersummary h3","top");
	Nifty("div#submit_internal h3","top");

	if (gSessionID.length == 0) {
		// if we dont have a sessionID, then we are using the static pages.
		console.log("Static version.");
// 		hide("ao_fav");
	}
	else {
		// if we DO have a sessionID, then we are using the dynamic pages.
		console.log("Dynamic version.");
	}

	setTimeout("InitRun()", 0);

	document.getElementById("mi_close").onclick=clk_mi_close;
}


function InitRun() {
	CheckStatus();
	console.assert(gReady >= 0);
	UpdateDisplayedList(0);
	console.assert(gMode == 99);
	gMode = 0;
	console.log("Startup Complete. displayed="+gDisplayed+", ready="+gReady);
}

// This function is used to initialise our article counts when we first start up.  
// It is also used to reset the display when needed.
function CheckStatus() {
	if (gSessionID.length <= 0) {
		gReady = gArticles.length;
		for (count=0; count<gArticles.length; count++) {
			gArticles[count][7]=0;
		}
	}
	else {
		console.assert(gMode == 99);
		gReady = 0;
		for (count=0; count<gArticles.length; count++) {
			console.assert(gArticles[count][7] == 0 || gArticles[count][7] == 1);
			if (gArticles[count][7] == 0) {
				gReady ++;
			}
		}
	}
	console.log("Finished Checking Status. gReady="+gReady+", total="+gArticles.length);
}

function GetArticleReadyCount() {
	gReady = 0;
	console.assert((gMode >= 0 && gMode <= 2) || gMode == 9);
	for (count = 0; count < gArticles.length ; count++) {
		console.assert(gArticles[count][8] >= 0 && gArticles[count][8] <= 1);
		console.assert(gArticles[count][7] == 0 || gArticles[count][7] == 1 || gArticles[count][7] == 8 || gArticles[count][7] == 9);
		if (gArticles[count][8] == 0) {
			if (gMode == 0 && gArticles[count][7] == 0) {
				if (gArticles[count][6] > 0 || gSessionID.length == 0) {
					gReady ++;
				}
			} else if (gMode == 1 && gArticles[count][7] == 1) {
				gReady ++;
			} else if (gMode == 2 && gArticles[count][7] != 9) {
				gReady ++;
			}
		}
	}
	console.assert(gReady >= 0);
}

// This function is called when we want to attempt to display more information to the screen.
function UpdateDisplayedList(incr) {
	console.assert(incr >= 0);

	console.log("Updating display list: incr="+incr+", displayed="+gDisplayed+", ready="+gReady);
	console.assert(gMode != 3);

	// we need to display more article entries, so go thru the array and display some that haven't already been displayed.
	var changed = 0;
	var more=incr;
	console.assert(gDisplayed >= 0);
	console.assert(gReady >= 0);
	console.assert(gArticles.length >= 0);
	for (var count = 0; count < gArticles.length && (gDisplayed < 15 || more > 0); count++) {
		if (gArticles[count][8] == 0) {
			if (((gMode == 0 && gArticles[count][7] == 0) && (gArticles[count][6] > 0 || gSessionID.length == 0)) ||
				(gMode == 1 && gArticles[count][7] == 1) ||
				(gMode == 2 && gArticles[count][7] != 9)) {

				DisplayArticleEntry(count);
				gArticles[count][8] = 1;
				gDisplayed ++;
				gReady --;
				changed ++;
				more --;
			}
		}
	}
	console.assert(gReady >= 0);
	console.assert(gDisplayed >= 0);

	// If we have less than 5 articles in the display, then we should attempt to add more to the list (which we might already have in our data array, or we might have to request more articles from the server).
	if (gReady < 5) {
		console.log("Updating display list: Requesting more.  ready="+gReady);
		RequestMore();
	}

	
	setTimeout("ProcMore()", 5);	

	if (changed > 0) { setTimeout("ProcVisited()", 0); }
	hide("loading");
}

function RequestMore() {
	console.assert(gQueryLimit > 0);
	console.log("gReady is "+gReady+", sending request for "+gQueryLimit+" articles");
	show("loading");
	ajaxReset();
	ajaxArg('limit', gQueryLimit);
	if (gSessionID.length > 0) { ajaxArg('sid', gSessionID); }
	ajaxRequest('/api/articles', resultArticles);
}

function Reload() {

	if (gMode == 3) {
		gMode = 9;
		SetModeTop();
	}
	else if (gMode == 4) {
		gMode = 9;
		SetModeFav();
	}
	else {
		gMode = 9;
		document.getElementById('t_articles').innerHTML = "";

		hide("more");
		show("loading");
		RequestMore();
		document.getElementById("ao_unread").className = "activelink";
		document.getElementById("ao_read").className = "";
		document.getElementById("ao_all").className = "";
		document.getElementById("ao_top").className = "";
		document.getElementById("ao_fav").className = "";
	}
}


function SetModeUnread() {
	if (gMode != 0) {
		console.log("Switching to Unread view");
		gMode = 0;
		ResetDisplay();
		UpdateDisplayedList(0);
		document.getElementById("ao_unread").className = "activelink";
// 		setTimeout("document.getElementById(\"ao_unread\").blur()", 1);
		document.getElementById("ao_read").className = "";
		document.getElementById("ao_all").className = "";
		document.getElementById("ao_top").className = "";
		document.getElementById("ao_fav").className = "";
	}
	else {
		Reload();
	}

}

function SetModeRead() {
	console.log("Switching to Read view");

	gMode = 1;
	ResetDisplay();
	UpdateDisplayedList(0);
	document.getElementById("ao_unread").className = "";
	document.getElementById("ao_read").className = "activelink";
// 	setTimeout("document.getElementById(\"ao_read\").blur()", 1);
	document.getElementById("ao_all").className = "";
	document.getElementById("ao_top").className = "";
	document.getElementById("ao_fav").className = "";
}

function SetModeAll() {
	console.log("Switching to All view");
	gMode = 2;
	ResetDisplay();
	UpdateDisplayedList(0);
	document.getElementById("ao_unread").className = "";
	document.getElementById("ao_read").className = "";
	document.getElementById("ao_all").className = "activelink";
// 	document.getElementById("ao_all").blur();
	document.getElementById("ao_top").className = "";
	document.getElementById("ao_fav").className = "";
}

function SetModeTop() {
	if (gMode != 3) {
		console.log("Switching to Top-100 view");
		gMode = 3;
		ResetDisplay();
		document.getElementById("ao_unread").className = "";
		document.getElementById("ao_read").className = "";
		document.getElementById("ao_all").className = "";
		document.getElementById("ao_top").className = "activelink";
		document.getElementById("ao_fav").className = "";
// 		document.getElementById("ao_top").blur();

		show("loading");
		hide("more");

		if (gLoadTop == 0) { dhtmlLoadScript("/top.js"); }
		setTimeout("TopRequest();", 5);
	}
	else {
		Reload();
	}
}

function SetModeFav() {
	if (gSessionID.length < 34) {
		alert("You must login for Favorites functionality to work");
	}
	else {
		if (gMode != 4) {
			console.log("Switching to Fave view");
			gMode = 4;
			ResetDisplay();
			document.getElementById("ao_unread").className = "";
			document.getElementById("ao_read").className = "";
			document.getElementById("ao_all").className = "";
			document.getElementById("ao_top").className = "";
			document.getElementById("ao_fav").className = "activelink";
	// 		document.getElementById("ao_top").blur();

			show("loading");
			hide("more");

			SendFavRequest();
		}
		else {
			Reload();
		}
	}
}



function TopRequest() {
	if (gLoadTop == 0) {
		console.log("top.js: not loaded yet.");
		setTimeout("TopRequest();", 5);
	}
	else {
		SendTopRequest();
	}
}

function ShowUserDetails(uid) {
	if (gLoadSummary == 0) {
		scroll(0,0);
		show("usersummary");
		console.log("summary.js: loading...");
		dhtmlLoadScript("/summary.js");
		gLoadSummary ++;
		setTimeout("ShowUserDetails("+uid+");", 5);
		setTimeout("Nifty(\"div#usersummary h3\",\"top\")", 0);
	}
	else if (gLoadSummary == 1) {
		console.log("summary.js: not loaded yet.");
		setTimeout("ShowUserDetails("+uid+");", 5);
	}
	else {
		mShowUserDetails(uid);
	}
}


function SetModeResults() {
	alert("This function is not available.");
}

// This function will look at how many articles we have displayed, and if it is less than a certain amount, but we have some ready, then display them.
function ProcMore() {
	console.assert(gReady >= 0);
	console.assert(gDisplayed >= 0);
	console.log("ProcMore(); gDisplayed="+gDisplayed+", gReady="+gReady+".");

	if (gDisplayed < 5 && gReady > 0) { ShowMore(); }
	show("more");
}

function resultArticles(xml) {
	var result = parseInt(xmlData(xml, 'result'));
	if (result == 1) {
		var articles = xml.getElementsByTagName('article');
		var visited = xmlData(xml, 'visited');

		console.log("Got "+articles.length+" articles");

		var count=0;
		while (count < articles.length) {
			AddArticle(articles[count]);
			count++;
		}

		if (count > 0) { SortArticles(); }
		if (articles.length >= gQueryLimit) { gQueryLimit += 100; }


 		CheckZeroScore();

		if (gMode == 9 || gMode == 99) {
			gMode = 0;
			ResetDisplay();
			UpdateDisplayedList(0);
			ProcMore();
		}

		console.log("resultArticles: list="+visited);
		ProcessVisitedList(visited);

		hide("loading");
	}
	else {
		alert("There was an error submitting your details: " + xmlData(xml, 'comment'));
	}
}

function AddArticle(article) {
	var done = 0;

	var id = parseInt(xmlData(article, 'id'));
	for (count = 0; count < gArticles.length && done == 0; count++) {
		if (gArticles[count][0] == id) {
			done = 1;
		}
	}

	if (done == 0) {
		var arc = gArticles.length;
		gArticles[arc] = [];
		gArticles[arc][0] = parseInt(xmlData(article, 'id'));
		gArticles[arc][1] = parseInt(xmlData(article, 'uid'));
		gArticles[arc][2] = xmlData(article, 'username');
		gArticles[arc][3] = xmlData(article, 'title');
		gArticles[arc][4] = xmlData(article, 'content');
		gArticles[arc][5] = xmlData(article, 'url');
		gArticles[arc][6] = parseInt(xmlData(article, 'score'));
		gArticles[arc][7] = 0;	// mode
		gArticles[arc][8] = 0;	// displayed
		if (gMode == 0 && (gArticles[arc][6] > 0 || gSessionID.length == 0)) {
			gReady ++;
		}
	}
}

// When sorting the list, we will do it by the score.  We want it to be sorted descendingly.
function SortMethod(a, b) {
	return(b[6] - a[6]);
}

function SortArticles() {
	console.log("Sorting Article List");
	console.assert(gMode != 3);
	if (gMode != 3) {
		gArticles.sort(SortMethod);
	}
}

function DisplayArticleEntry(count) {

	console.assert(gArticles != null);
	console.assert(count >= 0);
	console.assert(count < gArticles.length);

	var aid = gArticles[count][0];
	var uid = gArticles[count][1];
	var username = gArticles[count][2];
	var title = gArticles[count][3];
	var url = gArticles[count][5];
	var content = gArticles[count][4];

	DisplayArticle(aid, uid, username, title, url, content);
}

function DisplayArticle(aid, uid, username, title, url, content) {

	console.log("Displaying article #"+aid+": '"+title+"'");

	var errata = document.createElement('div');
	errata.className = "linkerrata";
	errata.innerHTML = "submitted by <a href=\"javascript:ShowUserDetails("+uid+");\">"+username+"</a>.";

	var viewlink = document.createElement('a');
	viewlink.className = "viewlink";

	var sArticleURL="";
	var sep = 0 + ( aid % 10 );
	if (gSessionID.length == 0) { sArticleURL = "http://rhokz.com/articles/"+sep+"/"+aid+"/"; }
	else { sArticleURL = "articles/"+aid+"/?sid="+gSessionID; }

	var sCommentsURL="";
	if (gSessionID.length == 0) { sCommentsURL = "http://rhokz.com/articles/"+sep+"/"+aid+"/comments.html"; }
	else { sCommentsURL = "articles/"+aid+"/comments/?sid="+gSessionID; }

	viewlink.href = sArticleURL;
	viewlink.innerHTML = title;
	viewlink.id = "alink_"+aid;
	if (gMode == 0) { viewlink.onmouseup=LinkUp; }

	var title = document.createElement('div');
	if (gMode == 0) { title.className = "linktitle"; }
	title.id = aid;
	title.appendChild(viewlink);

	var cmd = document.createElement('ul');
	cmd.className = "linkcmd";

	var star="star-grey.gif";
	var star_t="Add to Favorites List";
	if (IsFavSet(aid) == 1) {
		star="star.gif";
		star_t="Remove from Favorites List";
	}


	cmd.innerHTML = "<li><a href=\"javascript:Fave("+aid+");\"><img id=\"i_star_"+aid+"\" src=\"http://rhokz.com/images/icons/"+star+"\" alt=\""+star_t+"\" title=\""+star_t+"\"></a></li>"+
			"<li><a href=\""+sArticleURL+"\"><img src=\"http://rhokz.com/images/icons/page.gif\" alt=\"View Article\" title=\"View Article\"></a></li>"+
			"<li><a href=\""+sCommentsURL+"\"><img src=\"http://rhokz.com/images/icons/comment.gif\" alt=\"Comments\" title=\"Comments\"></a></li>"+
			"<li><a href=\"javascript:ShowUserDetails("+uid+");\"><img src=\"http://rhokz.com/images/icons/user.gif\" alt=\"Who submitted this article?\" title=\"Who submitted this article?\"></a></li>"+
			"<li><a href=\""+url+"\"><img src=\"http://rhokz.com/images/icons/link.gif\" alt=\"Direct Link\" title=\"Direct Link\"></a></li>"+
			"<li><a class=\"removelink\" href=\"javascript:RemoveLink("+aid+");\"><img src=\"http://rhokz.com/images/icons/delete.gif\" alt=\"Remove Article from List\" title=\"Remove Article from List\"></a></li>";

	var details = document.createElement('div');
	details.className = "linkdetails";
	details.innerHTML = content;

	var entry = document.createElement('div');
	entry.className = "linkrow";
	entry.id = "entry_" + aid;
	entry.appendChild(cmd);
	entry.appendChild(title);
	entry.appendChild(errata);
	entry.appendChild(details);

	var li = document.createElement('li');
	li.appendChild(entry);

	document.getElementById('t_articles').appendChild(li);
}

function incomplete() {
alert("That feature is not currently available.");
}

function Logout() {
	console.assert(gSessionID.length > 0);
	setCookie ("sid", "", 365, "rhokz.com");
	setTimeout("document.location.href='http://rhokz.com/main.html'", 1);
}

function ShowSubmitArticle() {
	hide('articles');
	show('submit');

	var fl = document.getElementById('f_submit');
	fl.title.value = "";
	fl.url.value = "";
	fl.details.value = "";
	fl.title.focus();
}


function ShowSubmitArticleInline() {
	show('submit_internal');
	setTimeout("Nifty(\"div#submit_internal h3\",\"top\")", 0);

	var fl = document.getElementById('f_submit2');
	fl.title.value = "";
	fl.url.value = "";
	fl.details.value = "";
	fl.title.focus();
}


function SubmitArticle() {

	var fl = document.getElementById('f_submit');
	if (fl.title.value.length < 10 || fl.details.value.length < 25) {
		alert("Your submission is too short, please elaborate.");
	}
	else if (fl.url.value.length < 5) {
		alert("The url doesnt seem to be valid.  Please check it.");
		fl.url.focus();
	}
	else {
		if (gSessionID.length < 34) {
			alert("You must be logged in to submit an article.");
		}
		else {
			ajaxReset();
			ajaxArg('sid', gSessionID);
			ajaxArg('title', fl.title.value);
			ajaxArg('url', fl.url.value);
			ajaxArg('details', fl.details.value);
			ajaxRequest('/api/newarticle', resultSubmitArticle, resultSubmitArticleError);

			hide("d_sub_1");
			show("d_sub_2");
		}
	}

	return false;
}

function resultSubmitArticle() {
	var result = parseInt(xmlData(xml, 'result'));
	if (result == 1) {
		alert("Article has been submitted successfully.  It can take up to 5 minutes for the article to applied to the site.");
		show('articles');
		hide("d_sub_2");
		show("d_sub_1");
		hide('submit');

		var vv = document.getElementById('u_submit_count');
		var vc = 0 + vv.innerHTML;
		vc ++;
		vv.innerHTML = vc;
	}
	else {
		alert("There was an error submitting your details: " + xmlData(xml, 'comment'));
	}
}

function resultSubmitArticleError(code) {
	alert("There was an error submitting the article.  It may have been submitted before the error occurred.  Error code: "+code);
}


function CancelSubmitArticle() {
 	var fl = document.getElementById('f_submit');
	if (fl.title.value.length > 0 || fl.url.value.length > 0 || fl.details.value.length > 0) {
		var res = confirm("You havent submitted this yet, are you sure you want to leave it?");
		if (res) {
			show('articles');
			hide('submit');
			hide("submit_internal");
		}
	}
	else {
		show('articles');
		hide('submit');
		hide("submit_internal");
	}
}

function ProcVisited() {

	if (gMode == 0) {
		var changed = 0;
		if (document.getElementById('articles').style.display != "none") {

			var list = getElementsByClassName('viewlink');
			console.log("ProcVisited: list length == "+list.length);
			for (count = 0; count < list.length; count++) {
				var ol = 0; var ot=0;
				ol = 0 + parseInt(list[count].offsetLeft);
				ot = 0 + parseInt(list[count].offsetTop);
				if (isNaN(ol) || isNaN(ot)) { ol = 0; ot = 0; }
//	 	  		alert("count:"+count+", offsetLeft:"+list[count].offsetLeft+", offsetTop:"+list[count].offsetTop+", ol="+ol+", ot="+ot);
				if (ol <= 0 && ot <= 0) {
					list[count].parentNode.parentNode.style.display = "none";
					list[count].className = "viewlinkrem";

					var aid = list[count].parentNode.id;
					console.assert(aid > 0);
					console.log("visited.  aid="+aid);

					var found=0;
					for (cc=0; cc < gArticles.length; cc++) {
						if (gArticles[cc][0] == aid) {
							found++;
							if (gArticles[cc][8] != 0) {
								gArticles[cc][8] = 0;
								gArticles[cc][7] = 1;
								cc=gArticles.length;
								gDisplayed --;
							}
						}
					}
					console.assert(found == 1);
					console.assert(gDisplayed >= 0);
					changed++;
				}
			}
		}

		if (changed > 0) {
			UpdateDisplayedList(0);
		}
	}
}


function LinkUp(e) {

	// We need to figure out... from the event.... what article ID this is for.
	var aid = this.parentNode.id;

	// then we need to check to see if this entry is already in our list.
	var found = 0;
	for (count=0; count < gVisitWaiting.length && found == 0; count++) {
		if (gVisitWaiting[count][0] == aid) {
			gVisitWaiting[count][1] = 60;
			found ++;
		}
	}

	if (found == 0) {
		var v=gVisitWaiting.length;
		gVisitWaiting[v] = [];
		gVisitWaiting[v][0] = aid;
		gVisitWaiting[v][1] = 60;
	}

	if (gLinkTimerID == 0) {
		gLinkTimerID = setTimeout("LinkTimer()", 1000);
	}
	ProcVisited();
}

function LinkTimer() {
	gLinkTimerID = 0;
	if (gVisitWaiting.length > 0) {
		ProcVisited();

		// if we are logged in, then send off an ajax request for a list of
		// visited articles (since the last time this was asked).
		if (gSessionID.length == 0) {
			gLinkTimerID = setTimeout("LinkTimer()", 1000);
		}
		else {
			var list = "";
			var cc = 0;
			for (count=0; count < gVisitWaiting.length; count++) {
				var aid = gVisitWaiting[count][0];
				if (aid > 0 && gVisitWaiting[count][1] > 0) {
					if (cc == 0) { list = aid; }
					else { list = list + "," + aid; }
					cc++;
				}
			}

			if (cc > 0) {
				console.assert(gSessionID.length >= 34);

				ajaxReset();
				ajaxArg('sid', gSessionID);
				ajaxArg('aid', list);
				ajaxRequest('/api/visited', resultVisited);
			}
		}
	}
}

function resultVisited(xml) {
	var result = parseInt(xmlData(xml, 'result'));
	if (result == 1) {
		var list = xmlData(xml, 'list');
		console.log("resultVisited: list="+list);
		ProcessVisitedList(list);
		DecrWaitCount();

		if (gVisitWaiting.length > 0) {
			if (gLinkTimerID == 0) {
				gLinkTimerID = setTimeout("LinkTimer()", 2000);
			}
		}

		var un = xmlData(xml, 'unvisited');
		ProcessUnvistedList(un);

		ProcVisited();
		ProcMore();
	}
	else { alert("There was an error submitting your details: " + xmlData(xml, 'comment')); }
}


function DecrWaitCount() {
	var count=0;
	while (count < gVisitWaiting.length) {
		gVisitWaiting[count][1] --;
		if (gVisitWaiting[count][1] <= 0) { gVisitWaiting.splice(count, 1); }
		else { count++; }
	}
}

function RemoveWaitEntry(aid) {
	var count=0;
	while (count < gVisitWaiting.length) {
		if (gVisitWaiting[count][0] == aid) {
			gVisitWaiting.splice(count, 1);
			console.log("Removed aid="+aid+" from Waiting list.  "+gVisitWaiting.length+" entries left");
		}
		else { count++; }
	}

}

function ProcessVisitedList(vlist) {
	// split the list, and go thru each one.
	var visited=vlist.split(",");
	var i=0;
	while(visited[i]) {
		var aid=parseInt(visited[i]);
//  		console.log("Visited: aid="+aid+", gMode="+gMode+", gReady="+gReady+", articles="+gArticles.length);

		for (count=0; count < gArticles.length; count++) {
			if (gArticles[count][0] == aid) {
				if (gMode == 0) {
//  					console.log("Article found: gReady="+gReady+", gDisplayed="+gDisplayed+", status="+gArticles[count][7]+", displayed="+gArticles[count][8]);
					if (gArticles[count][8] == 1) {
						gArticles[count][8] = 0;
						var entry = document.getElementById("entry_"+visited[i]);
						if (entry != null) { entry.style.display = 'none'; }
						console.assert(gDisplayed >= 0);
						gDisplayed --;
						console.assert(gDisplayed >= 0);
					}
					else if (gArticles[count][7] == 0) {
						console.assert(gReady >= 0);
						if (gArticles[count][6] > 0 || gSessionID.length == 0) {
							gReady --;
						}
						console.assert(gReady >= 0);
					}
					gArticles[count][7] = 1;
				}
				count = gArticles.length;
			}
		}

		console.assert(gReady >= 0);
		console.assert(gDisplayed >= 0);
		RemoveWaitEntry(visited[i]);
		console.assert(gReady >= 0);
		console.assert(gDisplayed >= 0);

		i++
	}
}


function ProcessUnvistedList(list) {
	// split the list, and go thru each one.
	var item=list.split(",");
	var i=0;
	while(item[i]) {
		var aid = parseInt(item[i]);
// 		console.log("Unvisited: aid="+aid+", gMode="+gMode+", gReady="+gReady);
		for (count=0; count < gArticles.length; count++) {
			if (gArticles[count][0] == aid) {
//				console.log("un: found article. aid="+aid+", score="+gArticles[count][6]+", status="+gArticles[count][7]+", displayed="+gArticles[count][8]+".");
				if (gArticles[count][7] == 8) { gArticles[count][7] = 0; }
				if (gArticles[count][7] == 0 && gArticles[count][6] == 0) {
					gArticles[count][6] ++;
					if (gMode == 0) {
						gReady ++;
					}
				}
				count = gArticles.length;
			}
		}

		i++
	}
	console.assert(gReady >= 0);
	console.assert(gDisplayed >= 0);
}


function ShowMore() {
	UpdateDisplayedList(10);
}


function RemoveLink(id) {
	console.assert(id > 0);
	console.assert(gArticles.length > 0);
	if (gMode != 4) {
		console.assert(gDisplayed > 0);
		var found=0;
		for (count=0; count<gArticles.length; count++) {
			if (gArticles[count][0] == id) {
				gArticles[count][7] = 9;
				gArticles[count][8] = 0;
				gDisplayed --;
				count = gArticles.length;
				found++;
			}
		}
		console.assert(found == 1);
		console.assert(gDisplayed >= 0);

		setTimeout("ProcMore()", 1);

		if (gSessionID.length > 0) {
			ajaxReset();
			ajaxArg('sid', gSessionID);
			ajaxArg('aid', id);
			ajaxRequest('/api/mark', resultVoid);
		}
	}
	else {
		console.assert(gDisplayed == 0);
		FaveRemove(id);
	}
	hide("entry_"+id);
	console.assert(gDisplayed >= 0);
}

function resultVoid() {
	var result = parseInt(xmlData(xml, 'result'));
	if (result != 1) { alert("There was an error submitting your details: " + xmlData(xml, 'comment')); }
}

function ResetDisplay() {
	document.getElementById('t_articles').innerHTML = "";
	for (count=0; count<gArticles.length; count++) {
		gArticles[count][8] = 0;
	}

	if (gMode != 3 && gMode != 4) {
		GetArticleReadyCount();
	}
	gDisplayed = 0;
	hide("more");
	if (gMode == 1 && gSessionID.length < 34) {
		var pp = document.createElement('p');
		pp.innerHTML = "The <b>Read</b> list will not be very accurate unless you are logged in.  The <b>Unread</b> list may also show articles you have read.  For the best experience, we recommend you <a href=\"createaccount.html\">create an account</a>, and login.  Its simple, easy, and non-invasive.";
		document.getElementById('t_articles').appendChild(pp);
	}
	console.assert(gReady >= 0);
}

function CheckZeroScore() {
	if (gSessionID.length > 0) {
		var list = "";
		var cc = 0;
		for (count=0; count<gArticles.length; count++) {
			if (gArticles[count][6] == 0) {
				if (cc == 0) { list = gArticles[count][0]; }
				else { list = list + "," + gArticles[count][0]; }
				cc++;
			}
		}

		console.log("CheckZeroScore: cc="+cc+", list="+list);

		if (cc > 0) {
			ajaxReset();
			ajaxArg('sid', gSessionID);
			ajaxArg('aid', list);
			ajaxRequest('/api/visited', resultVisited);
		}
	}
}


function forgotpass() {
	hide("login");
	show("login_forgot");
}

function ChangePassword() {
	hide("userdetails");
	show("changepass");
}

function CancelChangePass() {
	show('userdetails');
	hide('changepass');
	return false;
}

function lpm() {
	hide('lpm_a');
	show('login_pro_more1');
	show('login_pro_more2');
}

function clk_in_more() {
//	location.href="#moreinfo";
	hide("articles");
	show("moreinfo");
	Nifty("div#moreinfo h3", "top");
}

function clk_in_submit() {
}

function clk_in_feedback() {
	alert("That feature is not currently available.  We are working on it, please be patient.");
}

function clk_mi_close() {
	hide("moreinfo");
	show("articles");
}

function RemovePanel(id) {
	if (id == "welcome") {
		hide("intro");
		setCookie ("intro", "1", 365, "rhokz.com");
	}
	else if (id == "usersummary") {
		closesummary();
	}
	else if (id == "moreinfo") {
		clk_mi_close();
	}
	else if (id == "submit") {
		CancelSubmitArticle();
	}
}


function ShowLogin() {
	hide("login_forgot");
	show("login");
}


function ShowArticle(aid) {
alert("Show Article");
}


function IsFavSet(aid) {
	// we have a comma-delimited list of article ID's.  We need to check to see if this article id is in that list.  If so, return true, if not then return false.

	var result = 0;
	if (gFavList.length > 0) {
		var arr = gFavList.split(',');
		for (x in arr) {
			if (arr[x] == aid) {
				result = 1;
				break;
			}
		}
	}

	return(result);
}
