var color = "";
var items = new Array(100);
var topItems = new Array(10);
var topItemCount = 0;
var latestItems = new Array(10);
var latestItemCount = 0;
var ratedItems = new Array(10);
var ratedItemCount = 0;
var itemCount = 0;
var perPage = 6;
var currentPage = 1;
var maxPages = 0;
var modifiedDate = "&nbsp;";

function setupCurrentPage(cPage) { currentPage = getPage(); }
function setLastModifiedDate(cDate) { modifiedDate = cDate; }
function writeLastModifiedDate() { document.write(modifiedDate); }
function javaderimage(item)
{
   document.write("<td valign=top>");
   if (item.listjavaders != -1)
      document.write('<a href="' + item.listjavaders + '">');
   document.write("<img src='/~toyne/images/javadericon.gif' border=0 alt='Javaders listed at this site' width=24 height=16>");
   if (item.listjavaders != -1)
      document.write('<\a>');
   if (item.rateurl != "") tickimage("Rate Javaders @ " + item.name, item.rateurl);
   if (item.voteurl != "") tickimage("Vote for Javaders @ " + item.name, item.voteurl);
   document.write("</td>");
}
function tickimage(comment, url) { document.write("<a href='" + url + "' target=voterate><img src='/~toyne/images/tick.gif' border=0 alt='" + comment + "' width=14 height=13></a>"); }
function tickimagetd(comment, url) { document.write("<td align=middle><a href='" + url + "' onclick='return gotoRateLink(\"" + item.name + "\");' target=voterate><img src='/~toyne/images/tick.gif' border=0 alt='" + comment + "' width=14 height=13></a></td>"); }
function space() { document.write("&nbsp;"); }
function spacetd() { document.write("<td>&nbsp;</td>"); }

function addURL(id, name, isjavader, logo, url, desc, rateurl, voteurl, date, hits, rating, votes)
{
   var item = {id:id, name:name, listjavaders:isjavader, logo:logo, url:url, description:desc, rateurl:rateurl, voteurl:voteurl, date:date, hits:hits, rating:rating, votes:votes}
   items[itemCount++] = item;
   maxPages = parseInt((itemCount + perPage - 1) / perPage);
}

function addTop(itemName)
{
   if (topItemCount == 10) return;
   var item = findItemFromId(itemName);
   if (item == null) return;
   topItems[topItemCount++] = item;
}

function addLatest(itemName)
{
   if (latestItemCount == 10) return;
   var item = findItemFromId(itemName);
   if (item == null) return;
   latestItems[latestItemCount++] = item;
}

function addRated(itemName)
{
   if (ratedItemCount == 10) return;
   var item = findItemFromId(itemName);
   if (item == null) return;
   ratedItems[ratedItemCount++] = item;
}

function writeTopItems()
{
   var ilc = 0;
   for (ilc = 0; ilc < topItemCount; ilc++)
   {
      document.write('<tr><td valign="top"><img src="/~toyne/images/arrow.gif"></td>');
      document.write('<td nowrap valign="top"><font size=-2>');
      writeItemHREF(topItems[ilc]);
      document.write(' (' + topItems[ilc].hits + ')');
      document.write('</font></td></tr>');
   }
}

function writeLatestItems()
{
   var ilc = 0;
   for (ilc = 0; ilc < latestItemCount; ilc++)
   {
      document.write('<tr><td valign="top"><img src="/~toyne/images/arrow.gif"></td>');
      document.write('<td nowrap valign="top"><font size=-2>');
      writeItemHREF(latestItems[ilc]);
	if (latestItems[ilc].hits)
	      document.write(' (' + latestItems[ilc].hits + ')');
      document.write('</font></td></tr>');
   }
}

function writeRatedItems()
{
   var ilc = 0;
   for (ilc = 0; ilc < ratedItemCount; ilc++)
   {
      document.write('<tr><td valign="top"><img src="/~toyne/images/arrow.gif"></td>');
      document.write('<td nowrap valign="top"><font size=-2>');
      writeItemHREF(ratedItems[ilc]);
      document.write(' (' + ratedItems[ilc].rating + ')');
      document.write('</font></td></tr>');
   }
}

function writePageListing()
{
   document.write("<form name=quicklinkform>");
   document.write('<table cellspacing="1" cellpadding="5" width="100%">');
   document.write('<tr bgcolor="#B8B6A9"><th colspan="2">');
   writeComboBox();
   document.write('</th></tr>');
   document.write('<tr>');
   writeItems();
   document.write('</tr>');
   document.write('<tr><td colspan="4" align="center"><br><font size=-1>Click the <img src="/~toyne/images/tick.gif" width="14" height="13"> to vote/rate Javaders</font></td>');
   document.write('</tr></table>');
   document.write('</form>');
}

function writeItems()
{
   var ilc = (currentPage - 1) * perPage;
   for (; ilc < itemCount && ilc < (currentPage * perPage); ilc++)
      writeItem(items[ilc]);
}

function writeItem(item)
{
   // if (color == "#CECEBD") color = "#E1DDD1"; else color = "#CECEBD";
   color="#E1DDD1";
   document.write("<tr bgcolor=" + color + " style='background-Color:" + color + ";' onmouseover=\"this.style.backgroundColor='lightblue';return true;\" onmouseout=\"this.style.backgroundColor='" + color + "';return true;\">");
   // document.write("<tr bgcolor=" + color + " style='background-Color:" + color + ";cursor:hand' onclick='return gotoLink(\"" + item.name + "\");' onmouseover=\"this.style.backgroundColor='lightblue';window.status='Go to " + item.url + "';return true;\" onmouseout=\"this.style.backgroundColor='" + color + "';window.status=''; return true;\">");
   document.write("<td valign=top align=left>")
   // if (item.date != null) document.write(item.date);
   document.write("<table width=100%><tr bgcolor=#CECEBD><td align=left valign=top>")
   document.write("<font size=-1><a href='#' onclick='return gotoLink(\"" + item.name + "\");' target='" + item.name + "' onmouseover='window.status=\"Go to " + item.url + "\"; return true;' onmouseout='window.status=\"\"; return true;'>" + item.name + "</a></font>")
   writeRating(item);
   document.write("</td>")
   // if (item.logo != "") document.write("<td align=right valign=top><a href='#' onclick='return gotoLink(\"" + item.name + "\");' target='" + item.name + "' onmouseover='window.status=\"Go to " + item.url + "\"; return true;' onmouseout='window.status=\"\"; return true;'><img src='/~toyne/images/" + item.logo + "' border=0 alt='" + item.name + "'></a></td>")
   document.write("</tr></table>")
   if (item.description != "") document.write(item.description)
   document.write("</td>")
   if (item.listjavaders) javaderimage(item); else spacetd();
   document.write("</tr>");
}

function writeItemHREF(item)
{
   // document.write("<a href='#' title='" + stripTags(item.description) + "' onclick='return gotoLink(\"" + item.name + "\");' target='" + item.name + "' onmouseover='window.status=\"Go to " + item.url + "\"; return true;' onmouseout='window.status=\"\"; return true;'>" + item.name + "</a>")
   document.write("<a href='#' target='" + item.name + "' onmouseover='window.status=\"Go to " + item.url + "\"; return overlib(\"" + stripTags(item.description) + "\", CAPTION, \"" + item.name + "\");' onmouseout='window.status=\"\"; return nd();'>" + item.name + "</a>")
}

function writeRating(item)
{
   document.write("&nbsp;<font size=-2>");
   document.write("(Hits: " + item.hits + ", rating: " + item.rating + ", votes: " + item.votes);
   document.write(", <a href='#' onclick='return gotoRateLink(\"" + item.name + "\");' target='" + item.name + "' onmouseover='window.status=\"Rate " + item.name + "\"; return true;' onmouseout='window.status=\"\"; return true;'>rate it</a>)");
   document.write("</font>");
}

function writeComboBox()
{
   var ilc = 0;
   document.write("<table width=100%><tr><td align=left>");
   writeLinks();
   document.write("</td><td align=right>Quick link: <select name=list onchange='doit()'>");
   document.write("<option value=\" \"> --- Choose a site ---");
   for (ilc = 0; ilc < itemCount; ilc++)
      document.write("<option value=\"" + items[ilc].name + "\">" + items[ilc].name);
   document.write("</select></td></tr></table>")
}

function writeLinks()
{
   var ilc = 0;
   var pageLink = 1;
   document.write("Page: ");
   if (currentPage > 1)
      writeLink(currentPage - 1, " &lt;&lt; ");
   for (ilc = 0; ilc < itemCount; ilc+=perPage, pageLink++)
      if (pageLink == currentPage)
         document.write("<b>" + pageLink + "</b>&nbsp;");
      else
         writeLink(pageLink, pageLink);
   if (currentPage < maxPages)
      writeLink(currentPage + 1, " &gt;&gt; ");
}

function writeLink(toPage, toComment)
{
   document.write("<a href=netlistings.html?page=" + toPage + " onmouseover='window.status=\"Go to page " + toPage + "\"; return true;' onmouseout='window.status=\"\"; return true;'>" + toComment + "</a>&nbsp;");
}

function doit()
{
   if (document.forms.quicklinkform.list.value == " ") return;
   gotoLink(document.forms.quicklinkform.list.value);
}

function getPage()
{
   var location = new String(document.location);
   var currentPage = 1;
   if (location.indexOf("?page=") != -1)
      currentPage = parseInt(location.substring(location.indexOf("?page=") + 6));
   if (currentPage > maxPages)
      currentPage = 1;
   return currentPage;
}

function getRateId()
{
   var location = new String(document.location);
   if (location.indexOf("?id=") != -1) return location.substring(location.indexOf("?id=") + 4);
   return location;
}

function gotoRateLink(link)
{
   var ilc = 0;
   var item = null;
   var window = null;

   item = findItemFromName(link)
   if (item == null)
   {
      alert("Unable to locate required link in table, sorry!!");
      return false;
   }
   window = open("rate.html?id=" + item.id, item.id, "width=640,height=480,scrollbars=yes,resizeable=no");
   window.focus();
   return false;
}

function gotoLink(link)
{
   var item = findItemFromName(link);
   if (item == null)
   {
      alert("Unable to locate required link in table, sorry!! : " + link);
      return false;
   }
   document.forms.gotolink.target=item.name;
   document.forms.gotolink.redirect.value=item.url;
   document.forms.gotolink.subject.value="Netlistings go to site: " + item.id;
   document.forms.gotolink.id.value=item.id;
   document.forms.gotolink.submit();
   return false;
}

function findItemFromName(itemName)
{
   var ilc = 0;
   for (ilc = 0; ilc < itemCount; ilc++)
      if (itemName == items[ilc].name) return items[ilc];
   return null;
}

function findItemFromId(itemId)
{
   var ilc = 0;
   for (ilc = 0; ilc < itemCount; ilc++)
      if (itemId == items[ilc].id) return items[ilc];
   return null;
}

function stripSpace(string)
{
   var s = new String(string);
   var space = s.indexOf(" ");
   for (space = s.indexOf(" "); space > 0; space = s.indexOf(" "))
      s = s.substring(0, space) + s.substring(space + 1);
   return s;
}

function stripTags(string)
{
   var s = new String(string);
   var lt;
   var space = s.indexOf("'");
   for (space = s.indexOf("'"); space > 0; space = s.indexOf("'"))
      s = s.substring(0, space) + "`" + s.substring(space + 1);
   for (space = s.indexOf("<"); space > 0; space = s.indexOf("<"))
   {
      lt = s.indexOf(">");
      if (lt <= 0) s = s.substring(0, space); else s = s.substring(0, space) + s.substring(lt + 1);
   }
   return s;
}

function writeSiteName(item) { if (item == null) document.write("Item NULL"); else document.write(item.name); }
function writeSiteDescription(item) { if (item == null) document.write("Item NULL"); else document.write(item.description); }
