function winmessage() {
     if( gewonnen != 0 ) return;
     breite = document.bform.breite.options[document.bform.breite.options.selectedIndex].value;
     hoehe = document.hform.hoehe.options[document.hform.hoehe.options.selectedIndex].value;
     if( parseInt(breite) == 4 && parseInt(hoehe) == 4 ) {
         alert("glückwunsch! sie haben das format 4x4 gelöst.\n\ntipp: versuchen sie doch einmal ein anderes format,\nund ihr lösungsweg könnte in der rechten\ntabelle stehen");
         return;
     }
     settings = "B x H = " + breite + " x "+hoehe + ": ";
     for(setptr=0; setptr < (unDoPtr*2); setptr+=2) {
         settings+=unDo[setptr]+","+unDo[setptr+1]+" / ";
     }
     // Das semaphorFormular füllen und dann das Win Fenster aufrufen.
     text1 = "gluehlampe";
     text2 = "";
     text3 = "";
     text4 = 'loesung: ' + settings;
     // Nun den Namen erfragen un bei OK das Formular abschicken lassen
     oeffneListenFenster();

     gewonnen = 1;
}


function undone() {
         if( unDoPtr <= 0 ) return;
         unDoPtr--;  Ptr = unDoPtr * 2;
         old_x = unDo[Ptr]; old_y=unDo[Ptr+1]
         knipse(old_x,old_y,1);
}

function redone() {
         if( unDoPtr * 2 >= unDo.length ) return;
         Ptr = unDoPtr * 2; unDoPtr++;
         old_x = unDo[Ptr]; old_y=unDo[Ptr+1]
         knipse(old_x,old_y,1)
}

function init() {
        unDo = new Array; unDoPtr=0;
        gewonnen = 0;
        breite = document.bform.breite.options[document.bform.breite.options.selectedIndex].value;
        hoehe = document.hform.hoehe.options[document.hform.hoehe.options.selectedIndex].value;
        startx = Math.floor(C_COL/2-breite/2);
        starty = Math.floor(C_ROW/2-hoehe/2);
        endx=parseInt(breite) + startx; endy=parseInt(hoehe) + starty;
        for(zeile=0; zeile<C_ROW; zeile++){
           for(spalte=0; spalte<C_COL; spalte++){
              if( spalte>=startx && zeile>=starty && spalte<endx && zeile<endy ) {
                  setxy(spalte, zeile, aus)
              } else {
                  setxy(spalte, zeile, leer)
              }
           }
        }
}

function setxy(x,y,image) {
//         alert(x+","+y+"   "+((y*C_COL)+x))
         my_offset = parseInt(x) + parseInt(y) * parseInt(C_COL);
           document.images[firstfield+my_offset].src=image.src;
}

function test_end() {
         /* return = -1 == noch kein Ende */
         /* return = 1 == Ende */
        for(zeile=0; zeile<C_ROW; zeile++){
           for(spalte=0; spalte<C_COL; spalte++){
               if( status( spalte, zeile ) == 0 ) return -1;
           }
        }
        return 1;
}

function knipse(x,y,done) {
         if( toggle(x,y) == -1 ) return;
         toggle(x,y-1);
         toggle(x,y+1);
         toggle(x+1,y);
         toggle(x-1,y);
         if( done != 1 ) {
                  if( unDoPtr*2 < unDo.length ) unDo = unDo.slice(0,unDoPtr*2);
                  unDo = unDo.concat(new Array(x,y)); unDoPtr++;
         }
         if( test_end() != -1 ) {
             // Nur dann Winmessage, wenn nicht ein Lösungsweg angezeigt wird
             winmessage();
         }
}

function toggle(x,y) {
         if( (stat = status(x,y)) == -1 ) return -1;
         if( stat == 1 ) setxy(x,y,aus);
         if( stat == 0 ) setxy(x,y,an);
         return 1;
}

function status(x,y) {
         /* gibt den Status des Feldes x,y wieder */
         /* -1 = fehler */
         /* 0 = aus */
         /* 1 = an */
         breite = document.bform.breite.options[document.bform.breite.options.selectedIndex].value;
         hoehe = document.hform.hoehe.options[document.hform.hoehe.options.selectedIndex].value;
         startx = Math.floor(C_COL/2-breite/2);
         starty = Math.floor(C_ROW/2-hoehe/2);
         endx=parseInt(breite) + startx; endy=parseInt(hoehe) + starty;
         if( x < startx || x >= endx || y < starty || y >= endy ) return -1;
         if( document.images[firstfield+(y*C_COL)+x].src.indexOf(animg) != -1 ) return 1;
         if( document.images[firstfield+(y*C_COL)+x].src.indexOf(ausimg) != -1 ) return 0;
         return -1;
}

function solution(x,y,s_arr) {
         if( !confirm("schauen sie sich die lösung für das "+x+"x"+y+" feld\nmit 'zug vor' und 'zug zurück' an.") ) return;
         document.bform.breite.options[x-3].selected = true;
         document.hform.hoehe.options[y-3].selected = true;
         init();
         unDoPtr = 0; unDo = s_arr;
         gewonnen = 1; //bei lösungsweganzeige keine winmessage bringen
}
