/* Mega Man 2 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 Bisqwit
 */
function mm2input(name)
{var tH=this,defs = {
    gel: function(id)   { return document.getElementById(name+id) },
    id:  function(id)   { return tH.gel(id).selectedIndex },
    ids: function(id,v) { tH.gel(id).selectedIndex=v },
    ch:  function(id)   { return tH.gel(id).checked },
    chs: function(id,v) { tH.gel(id).checked = v>0 },
    view:function(id,v) { tH.gel(id).style.visibility = v?'visible':'hidden' },
    img: function(id,v) { tH.gel(id).src = v },
    
    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],
    map:   [[3*5+5, 1*5+2, 'H'],
            [3*5+2, 4*5+3, 'A'],
            [1*5+5, 3*5+3, 'W'],
            [2*5+3, 3*5+1, 'B'],
            [2*5+4, 1*5+4, 'Q'],
            [4*5+4, 2*5+1, 'F'],
            [4*5+1, 4*5+5, 'M'],
            [4*5+2, 2*5+5, 'C']],
    
    count:0,
    
  updatedots: function()
  {
    var n,files = ['css/black.png', 'css/red.png']
    tH.count=0
    for(n=0;n<25;++n)
    {
      var s = tH.status[n]
      tH.img('d'+n, files[s])
      if(s>0) ++tH.count
    }
    tH.img('n', 'css/tmpl_'+tH.count+'.png')
  },
  decodehelper:function(e,n)
  {
    var pos,a = 0
    pos=tH.map[n][1]; a += tH.status[5+(e + pos - 6)%20]
    pos=tH.map[n][0]; a -= tH.status[5+(e + pos - 6)%20]
    tH.chs(tH.map[n][2], a);
    return a
  },
  encodehelper:function(e,n)
  {
    var id  = tH.map[n][2]
    var val = tH.ch(id) ? 1 : 0
    var pos = tH.map[n][val]
    tH.status[5+(e + pos - 6)%20] = 1
  },
  decode:function()
  {
    var e,n,error = 0;
    
    e = -1
    for(n=0; n<5; ++n) if(tH.status[n]) { if(e==-1){e=n;}else{error=1} }
    if(e < 0) { e=0; error=1; }
    
    tH.ids('E', e)
    
    for(n=0; n<8; ++n) error += !tH.decodehelper(e, n);
    
    tH.view('error', error)
  },
  hitdot:function(n)
  {
    var s = (tH.status[n]+1) % 2
    if(s==1 && tH.count >= 9) { return }
    tH.status[n]=s

    tH.updatedots()
    tH.decode()
  },
  setup:function()
  {
    var e,n;
    for(n=0;n<25;++n) tH.status[n]=0
    e=tH.id('E')
    tH.status[e]=1
    for(n=0; n<8; ++n) tH.encodehelper(e, n);
    tH.updatedots()
    tH.decode()
  }
}for(var i in defs) this[i]=defs[i]
}
