/* Mega Man 3 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 mm3input(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 },
 
    eplaces:[3*6+5, 5*6+6, 5*6+4, 2*6+4, 1*6+5,
             3*6+1, 4*6+2, 3*6+3, 6*6+2, 1*6+6],

    /* 1red=a, 2any=b, 1blue=ab, none=none */
    grps:    [[1*6+3, 6*6+6, 'TO','SN'],
              [6*6+4, 4*6+6, 'SP','SH'],
              [4*6+3, 6*6+5, 'NE','MA'],
              [2*6+5, 3*6+4, 'GE','HA'],
              [1*6+1, 1*6+4, 'X1','X2'],
              [2*6+2, 2*6+6, 'X3','X4'],
              [6*6+3, 5*6+1, 0,   'BR']],
  
    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],

  updatedots: function()
  {
    files = ['css/black.png', 'css/purple.png', 'css/blue.png', 'css/violet.png']
    violet = 3
    for(n=0;n<36;++n)
    {
      s = tH.status[n]
      //if(s > 0 && s < violet) { violet = s; }
    }
    for(n=0;n<36;++n)
    {
      s = tH.status[n]
      if(s == 3) { s = violet; tH.status[n] = s; }
      tH.img('d'+n, files[s])
    }
  },
  
  decodehelper: function(n)
  {
    pos = tH.grps[n]
    c1 = [0,1,3,4]; a = c1[tH.status[pos[0]-7]]
    c2 = [0,2,2,2]; b = c2[tH.status[pos[1]-7]]
    
    n = a+b
    if(a && b) n=4
    
    /* Note: MSIE doesn't like it if you rename a and b to A and B. */
    a=pos[2]; if(a)tH.chs(a, n&1)
    b=pos[3]; if(b)tH.chs(b, n&2)

    return n
  },
  encodehelper: function(n)
  {
    pos = tH.grps[n]
    types =[0,1,3,2]
    select=[0,0,1,0]
    
    n=0
    /* Note: MSIE doesn't like it if you rename a and b to A and B. */
    a=pos[2]; if(a) n += tH.ch(a)
    b=pos[3]; if(b) n += tH.ch(b)*2
    
    ind = select[n]
    tH.status[pos[ind]-7] = types[n]
  },
  
  decode: function()
  {
    error = 0
    
    dotcount=0
    for(n=0; n<36; ++n) if(tH.status[n]) ++dotcount

    e=-1
    for(n=0; n<10; ++n) if(tH.status[tH.eplaces[n]-7]) { if(e==-1){e=n;}else{error=1} }
    if(e < 0) {e=0; error=1; }
    tH.ids('E', e)
    --dotcount
    
    grp1 = 0
    for(n=0; n<4; ++n) { a=tH.decodehelper(n); if(a>3)error=1; grp1 += a; if(a)--dotcount }

    grp2 = 0
    for(n=4; n<6; ++n) { a=tH.decodehelper(n); if(a>3)error=1; grp2 += a; if(a)--dotcount }

    grp3 = 0
    a=tH.decodehelper(6); if(a!=0 && a!=2)error=1; grp3 += a; if(a)--dotcount
    
    if(grp2 > 0 && grp1 != 3*4) error=1;
    
    if(grp3 > 0 && (grp2 != 3*2 || grp1 != 3*4)) error=1;
    
    if(dotcount != 0) error=1
    
    tH.view('error', error)
  },
  hitdot: function(n)
  {
    s = tH.status[n]+1;
    if(s>=3) s=0;
    tH.status[n]=s;

    tH.updatedots()
    tH.decode()
  },
  
  setup: function()
  {
    for(n=0;n<36;++n) tH.status[n]=0
    
    tH.status[tH.eplaces[tH.id('E')]-7]=3
    
    for(n=0;n<7;++n) tH.encodehelper(n)

    tH.updatedots()
    tH.decode()
  }
}for(var i in defs) this[i]=defs[i]
}
