/* Mega Man 6 password generator / decoder
 * Copyright (C) 1992,2004 Joel Yliluoma (http://iki.fi/bisqwit/)
 * Written for http://tasvideos.org/PasswordGenerators.html
 * Permission to copy and modify is granted under the following terms:
 *   The copyright notice is kept unmodified
 *   No attempts are made to prevent anyone downloading the source code
 *
 * Reverse engineering by unknown, documentation from Protoman's site
 */
function mm6input(name)
{
  this.ebdot = [6*6+5, 5*6+5]
  this.grps =
  [
    /* basic binary pattern: --- a-- -b- ab- --c a-c -bc abc */
    /* first line is used if has EB, first otherwise. */
    [1*6+2, 3*6+2, 5*6+2, 2*6+6, 0*6+0, 0*6+0, 4*6+4, 6*6+4,
     1*6+1, 1*6+2, 2*6+1, 2*6+2, 0*6+0, 0*6+0, 4*6+1, 4*6+2,
     'BL', 'TO', 'B'],

    [2*6+1, 2*6+3, 2*6+5, 4*6+1, 0*6+0, 0*6+0, 4*6+3, 4*6+6,
     5*6+1, 5*6+2, 6*6+1, 6*6+2, 0*6+0, 0*6+0, 2*6+3, 2*6+4,
     'WI', 'YA', 'E'],

    [1*6+3, 1*6+4, 1*6+5, 5*6+3, 0*6+0, 0*6+0, 3*6+4, 5*6+4,
     1*6+5, 1*6+6, 2*6+5, 2*6+6, 0*6+0, 0*6+0, 4*6+5, 4*6+6,
     'PL', 'KN', 'A'],

    [2*6+2, 1*6+1, 3*6+1, 5*6+1, 0*6+0, 0*6+0, 6*6+1, 6*6+3,
     3*6+3, 3*6+4, 4*6+3, 4*6+4, 0*6+0, 0*6+0, 6*6+3, 6*6+4,
     'FL', 'CE', 'T']
  ]

  this.status=[0,0,0,0,0,0,
               0,0,0,0,0,0,
               0,0,0,0,0,0,
               0,0,0,0,0,0,
               0,0,0,0,0,0,
               0,0,0,0,0,0]

  this.gel = function(id)   { return document.getElementById(name+id) }
  this.id  = function(id)   { return this.gel(id).selectedIndex }
  this.ch  = function(id)   { return this.gel(id).checked }
  this.chs = function(id,v) { this.gel(id).checked=v>0 }
  this.view= function(id,v) { this.gel(id).style.visibility = v?'visible':'hidden' }
  this.img = function(id,v) { this.gel(id).src = v }
  
  this.updatedots = function()
  {
    files = ['css/black.png', 'css/red.png']
    for(n=0;n<36;++n)
    {
      s = this.status[n]
      this.img('d'+n, files[s])
    }
  }
  
  this.decodehelper = function(n,eb)
  {
    grp = this.grps[n]
    
    a=-1
    for(n=0; n<8; ++n)
    {
      d = grp[n + eb*8]
      if(d != 0)
        if(this.status[d-7])
        {
          if(a==-1){a=n;}else{error=1;}
        }
    }
    if(a==-1) { return 1 }
    
    this.chs(grp[16], a&1)
    this.chs(grp[17], a&2)
    this.chs(grp[18], a&4)
    
    return error
  }
  this.encodehelper = function(n,eb)
  {
    grp = this.grps[n]
    
    pos = this.ch(grp[16])*1 + this.ch(grp[17])*2 + this.ch(grp[18])*4 + eb*8
    pos = grp[pos]
    
    if(pos > 0) this.status[pos-7] = 1
  }
  
  this.decode = function()
  {
    error = 0
    
    dotcount=0
    for(n=0; n<36; ++n) if(this.status[n]) ++dotcount
    if(dotcount != 5) error=1
    
    eb = this.status[this.ebdot[1]-7] - this.status[this.ebdot[0]-7]
    if(eb==0) error=1
    if(eb<0) eb=0
    
    for(n=0;n<4;++n) error += this.decodehelper(n, eb)
    this.chs('EB', eb)
    
    this.view('error', error)
  }
  this.hitdot = function(n)
  {
    s = (this.status[n]+1) % 2
    this.status[n]=s

    this.updatedots()
    this.decode()
  }
  
  this.setup = function()
  {
    for(n=0;n<36;++n) this.status[n]=0
    
    eb = this.ch('EB') ? 1 : 0
    for(n=0;n<4;++n) this.encodehelper(n,eb)
    this.status[this.ebdot[eb]-7] = 1

    this.updatedots()
    this.decode()
  }
}

