/***************************************************************************
 *   Copyright (C) 2008, Paul Lutus                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

var copyright = "Copyright &copy; 2008, P. Lutus &mdash; http://www.arachnoid.com";

var extended = true;

var ctlString = new Array(
  "NUL",
  "SOH",
  "STX",
  "ETX",
  "EOT",
  "ENQ",
  "ACK",
  "BEL",
  " BS",
  " HT",
  " LF",
  " VT",
  " FF",
  " CR",
  " SO",
  " SI",
  "DLE",
  "DC1",
  "DC2",
  "DC3",
  "DC4",
  "NAK",
  "SYN",
  "ETB",
  "CAN",
  " EM",
  "SUB",
  "ESC",
  " FS",
  " GS",
  " RS",
  " US"
);

function wrapTag(a,b,c) {
  if(c == null) {
    return "<" + b + ">" + a + "</" + b + ">\r\n";
  }
  else {
    return "<" + b + " " + c + ">" + a + "</" + b + ">\r\n";
  }
}

function baseDisp(v,b,n) // value, base, number of displayed digits
{
  var digitString = "0123456789abcdef";
  var s = "";
  do {
    s = digitString.charAt(v % b) + s;
    v = Math.floor(v/b);
  }
  while(--n > 0);
  return s;
}

function makeTable()
{
  var columns = 8;
  var rows = (extended)?32:16;
  var s = "";
  var r = "";
  var base = new Array(16,10,8);
  var nlen = new Array(2,3,3);
  var colName = new Array("Hex/Dec/Oct","Ch");
  for(var h = 0;h < columns;h++) { // heading line
    for(var i = 0;i < colName.length;i++) {
      r += wrapTag(colName[i],"td","style=\"text-align:center\"");
    }
  }
  s += wrapTag(r,"TR");
  for(var v = 0;v < rows;v++) { // each row
    r = "";
    for(var h = 0;h < columns;h++) { // each character
      var c = v + (h * rows);
      
      var qn = "";
      for(var b = 0; b < base.length;b++) { // each number base
        qn += ((b > 0)?"/":"") + baseDisp(c,base[b],nlen[b]);
      }
      r += wrapTag(qn,"TD","style=\"text-align:center\"");
      if(c >= 127 && c < 160) {
        c = 32;
      }
      var qc = (c < 32)?ctlString[c]:String.fromCharCode(c);
      r += wrapTag(qc,"TD","style=\"text-align:center\"");
    }
    s += wrapTag(r,"TR");
  }
  
  var title = "Extended ASCII Character Set";
  title = wrapTag(title,"h2") + wrapTag(copyright,"p") + "<p></p>";
  s = title + wrapTag(s,"TABLE","bordercolor=\"#0000ff\" bgcolor=\"#ffffe1\" border=1 cellpadding=2 cellspacing=0");
  return wrapTag(s,"CENTER") + "<p></p>";
}
