function ShowPageControls()
{
    var sf = queryString('sf');
    if (sf =='false')
        sf = 3;
    var sd = queryString('sd');
    if (sd == 'false')
        sd = 'a';
        
    document.write ("<div class=paginate>");
    document.write ("<table border=0 width=100% cellpadding=0 cellspacing=0><tr>");
    var currenturl = pageid + ".html?";
    
    document.write ("<td><b>Sort by:</b>&nbsp;&nbsp;&nbsp;");
    for (j = 0; j < SortFields.length; j++)
    {
    	document.write ("<a href='" + currenturl + "sf=" + (j+1) + "&sd=a'>");
    	document.write (SortFields[j]);
    	document.write ("</a>&nbsp;&nbsp;&nbsp;");
    }
    document.write ("</td>");

    if ( pages.length > 1 && CurrentPage > 0 ) 
    {
      document.write ("<td nowrap style='background-color: white' align=right>");
      document.write ( PageDisplay[CurrentPage - 1] );
      document.write ("</td>");
    }
    document.write("</tr></table>");
    document.write ("<table border=0 width=100% cellpadding=0 cellspacing=0><tr>");

    if ( pages.length > 1 && CurrentPage > 0 ) 
    {
      document.write ("<td nowrap align=right>" );
      document.write ("<a href=" + pageid + ".html?page=0&sf=" + sf + "&sd=" + sd + ">VIEW ALL</a> | ");
      if ( CurrentPage != 1 )
      {
          document.write ("<a href=" + pageid + ".html?page=" + String(CurrentPage-1) + "&sf=" + sf + "&sd=" + sd + ">PREV</a>&nbsp;|&nbsp;")
      }
      for (var i = 0; i < pages.length; i++ )
      {
        document.write(" ");
        if ( i != CurrentPage - 1 )
        {
          document.write ("<a href=" + pageid + ".html?page=" + String(i + 1) + "&sf=" + sf + "&sd=" + sd + ">" + (i+1) + "</a>")
        }
        else
        {
          document.write ("<a href=" + pageid + ".html?page=" + String(i + 1) + "&sf=" + sf + "&sd=" + sd + "><b>" + String(i+1) + "</b></a>")
        }
        document.write(" ");
      }
      if ( CurrentPage != pages.length )
      {
          document.write ("|&nbsp;<a href=" + pageid + ".html?page=" + String(CurrentPage+1) + "&sf=" + sf + "&sd=" + sd + ">NEXT</a>&nbsp;")
      }
      document.write ("</td>");
    }
    document.write ("</table>");
    document.write ("</div>");
}

function ShowPage()
{
  ShowPageControls();
  document.write("<br>");
  document.write("<table id=header border=0 cellpadding=0 cellspacing=0 width=100% bgcolor=#78A2CE><tr><td colspan=3>&nbsp;</td><td align=right width=100>");
  document.write("<a href=" + pageid + ".html?sf=2&sd=a>Price</a></td><td align=right width=150><a href=" + pageid + ".html?sf=3&sd=a>" + SortFields[2] + "</a></td></tr></table>");
  if ( CurrentPage > 0 )
  {
    document.write( String(pages[CurrentPage - 1]).replace(/`/gi, "'") );
  }
  else
  {
    for (var i = 1; i <= pages.length; i++ )
    {
        document.write( String(pages[i - 1]).replace(/`/gi, "'") );
    }
  }
  document.write("<br>");
  ShowPageControls();
}

function PageItem(text,publisher,price,publicationdate)
{
    this.text = text;
    this.publisher = publisher;
    this.price = price;
    if (SortFields[2] == 'Published')
    {
        dateparts = publicationdate.match("(.*) (.*)");
        if (dateparts && dateparts.length == 3)
        {
            this.publicationdate = new Date(dateparts[1] + " 1, " + dateparts[2]);
        }
        else
	    this.publicationdate = publicationdate;
    }
    else
        this.publicationdate = publicationdate;

}
function PageSortFunction(a,b)
{
    var sf = queryString('sf');
    if (sf =='false')
        sf = 3;
    var sd = queryString('sd');
    if (sd == 'false')
        sd = 'a';
	if (sf == '1')
	{
	    if (a.publisher < b.publisher)
	        return (-1);
	    else if (a.publisher > b.publisher)
	        return (1);
	    else
	        return (0);
	}
	if (sf == '2') // price
	{
	    return (a.price - b.price);
	}
	if (sf == '3')
	{
	    if (a.publicationdate > b.publicationdate)
	        return (-1);
	    else if (a.publicationdate < b.publicationdate)
	        return (1);
	    else
	        return (0);
	}
}

function SortPage(wid, cols)
{
	var n = PageItems.length;
	var nPages = Math.ceil(n / PageSize);
	
	PageItems.sort(PageSortFunction);
	
	for (var j = 0; j < nPages; j++)
	{
		var ItemsOnPage = (j * PageSize + PageSize > n ? n - (j*PageSize) : PageSize);
		PageDisplay[j] = "Displaying <b>" + (j * PageSize + 1) + "</b> to <b>" + ( j * PageSize + ItemsOnPage) + "</b> (of <b>" + n + "</b> products)";
		pages[j] = "<table border=0 width=" + wid + " cellpadding=0 cellspacing=0>";
		for (var rows = 0; rows < Math.ceil(ItemsOnPage / cols); rows++)
		{
			pages[j] += "<tr valign=top ";
			pages[j] += ((j+rows) % 2 ? " bgcolor=#EFEFEF" : " bgcolor=#FFFFFF") + ">";
			for (var columns = 0; columns < cols; columns++)
			{
				pages[j] += "<td>";
				var idx = j * PageSize + rows * cols + columns;
				if (idx < n)
					pages[j] += PageItems[idx].text;
				pages[j] += "</td>";
			}
			pages[j] += "</tr>";
		}
		pages[j] += "</table>";
	}
	ShowPage();
}
