
/* Copyright (c) 2003 Timothy Sakharov, Alexander Sakharov */

var i;
var j;
var k;
var last;

var cnt;
var maxim;
var was;

var sum;
var url;
var sr;
var n;

var height;
var width;
var imgInd;

function size_strt_f() {

    ind = document.theForm_strt.total.selectedIndex;
    itemText = document.theForm_strt.total.options[ind].text;
    itemValue = document.theForm_strt.total.options[ind].value;
    if ( itemValue.charAt(0) == '-' ) {
      alert("Incorrect selection");
      return false;
    }
    height = itemValue.charAt(0);
    width = itemValue.charAt(1);

    sr = location.search;
    sr = sr.substring(sr.indexOf("SRC="));

    if ( sr.length < 9 || sr.charAt(8) != 'a' || sr.charAt(11) != 'v' ) {
          total = -1;
    }

    location.replace("http://sakharov.net/puzzle/gunport.html?SRC=sakharov&DIM=" + height + width);
}

// --------------------

function load_f() {

  last = 0;
  imgInd = new Array(9);
  for (i = 0; i < 9; i++) {
    imgInd[i] = new Array(9);
  }
  var r;
  for (r = 0; r < document.images.length; r++) {
      if ( document.images[r].name.substring(0, 7) == "problem" ) {
        var fr = document.images[r].name.charAt(7);
        var sc = document.images[r].name.charAt(8);
        imgInd[fr][sc] = r;
      }
  }

  maxim = new Array(9);
  for (i = 0; i < 9; i++) {
    maxim[i] = new Array(9);
  }
  maxim[3][3] = 1;
  maxim[3][4] = 0;
  maxim[3][5] = 1;
  maxim[3][6] = 2;
  maxim[3][7] = 1;
  maxim[3][8] = 2;
  maxim[4][4] = 0;
  maxim[4][5] = 2;
  maxim[4][6] = 2;
  maxim[4][7] = 2;
  maxim[4][8] = 2;
  maxim[5][5] = 1;
  maxim[5][6] = 2;
  maxim[5][7] = 3;
  maxim[5][8] = 4;
  maxim[6][6] = 4;
  maxim[6][7] = 4;
  maxim[6][8] = 4;
  maxim[7][7] = 5;
  maxim[7][8] = 6;
  maxim[8][8] = 8;

  var dim = location.search;
  var n = dim.indexOf("DIM=");
  height = dim.charAt(n+4);
  width = dim.charAt(n+5);

  for (i = 0; i < 8; i++) {
    for (j = 0; j < 8; j++) {
      if ( i >= height || j >= width ) {
        k = findImage(i+1, j+1);
        document.images[k].src = "../image/blue.gif";
      }
    }
  }

  removeMode = false;
}

function findImage(m, n) {
  return imgInd[m][n];
      /*
      var r;
      for (r = last; r < document.images.length; r++) {
        if ( document.images[r].name == "problem" + m + n ) {
          last = r;
          return r;
        }
      }
      for (r = 0; r < last; r++) {
        if ( document.images[r].name == "problem" + m + n ) {
          last = r;
          return r;
        }
      }
      return -1;
      */
}

function removeTile_dummy(m, n) {
  ;
}

function remove_f() {
  if ( document.theForm.remove.checked ) {
    removeMode = true;
  } else {
    removeMode = false;
  }
}

function removeTile(m, n) {
      var p;
      //alert("remove " + m + n);
      sr = location.search;
      sr = sr.substring(sr.indexOf("SRC=")+4);
      if ( sr.length < 8 || sr.charAt(3) != 'h' ) {
          return;
      }

      i = findImage(m, n);
      if ( document.images[i].src.indexOf("orange") >= 0 ) {
            if ( document.images[i].src.indexOf("oranger") >= 0 ) {
              was = 'r';
              p = findImage(m, n+1);
              document.images[p].src = "../image/yellow.gif";
            } else if ( document.images[i].src.indexOf("orangeb") >= 0 ) {
              was = 'b';
              p = findImage(m+1, n);
              document.images[p].src = "../image/yellow.gif";
            } else if ( document.images[i].src.indexOf("orangel") >= 0 ) {
              was = 'l';
              p = findImage(m, n-1);
              document.images[p].src = "../image/yellow.gif";
            } else if ( document.images[i].src.indexOf("oranget") >= 0 ) {
              was = 't';
              p = findImage(m-1, n);
              document.images[p].src = "../image/yellow.gif";
            } else {
              was = 'y';
            }
            document.images[i].src = "../image/yellow.gif";
      }
}

function canSlide() {
  var p;
  var q;
  for (i = 0; i < 8; i++) {
    for (j = 0; j < 8; j++) {
      k = findImage(i+1, j+1);

      if ( document.images[k].src.indexOf("oranger") >= 0 ) {

        if ( j > 0 ) {
          p = findImage(i+1, j);
          if ( document.images[p].src.indexOf("yellow") >= 0 ) {
            return true;
          }
        }
        if ( j+2 < 8 ) {
          p = findImage(i+1, j+3);
          if ( document.images[p].src.indexOf("yellow") >= 0 ) {
            return true;
          }
        }
        if ( i > 0 ) {
          p = findImage(i, j+1);
          q = findImage(i, j+2);
          if ( document.images[p].src.indexOf("yellow") >= 0
          && document.images[q].src.indexOf("yellow") >= 0 ) {
            return true;
          }
        }
        if ( i+1 < 8 ) {
          p = findImage(i+2, j+1);
          q = findImage(i+2, j+2);
          if ( document.images[p].src.indexOf("yellow") >= 0
          && document.images[q].src.indexOf("yellow") >= 0 ) {
            return true;
          }
        }

      } else if ( document.images[k].src.indexOf("orangeb") >= 0 ) {

        if ( i > 0 ) {
          p = findImage(i, j+1);
          if ( document.images[p].src.indexOf("yellow") >= 0 ) {
            return true;
          }
        }
        if ( i+2 < 8 ) {
          p = findImage(i+3, j+1);
          if ( document.images[p].src.indexOf("yellow") >= 0 ) {
            return true;
          }
        }
        if ( j > 0 ) {
          p = findImage(i+1, j);
          q = findImage(i+2, j);
          if ( document.images[p].src.indexOf("yellow") >= 0
          && document.images[q].src.indexOf("yellow") >= 0 ) {
            return true;
          }
        }
        if ( j+1 < 8 ) {
          p = findImage(i+1, j+2);
          q = findImage(i+2, j+2);
          if ( document.images[p].src.indexOf("yellow") >= 0
          && document.images[q].src.indexOf("yellow") >= 0 ) {
            return true;
          }
        }

      }
    }
  }
  return false;
}

function existPair() {
  var p;
  var q;
  for (i = 0; i < 8; i++) {
    for (j = 0; j < 8; j++) {
      k = findImage(i+1, j+1);

      if ( document.images[k].src.indexOf("yellow") >= 0 ) {

        if ( j > 0 ) {
          p = findImage(i+1, j);
          if ( document.images[p].src.indexOf("yellow") >= 0 ) {
            return true;
          }
        }
        if ( j+2 < 8 ) {
          p = findImage(i+1, j+2);
          if ( document.images[p].src.indexOf("yellow") >= 0 ) {
            return true;
          }
        }
        if ( i > 0 ) {
          p = findImage(i, j+1);
          if ( document.images[p].src.indexOf("yellow") >= 0 ) {
            return true;
          }
        }
        if ( i+2 < 8 ) {
          p = findImage(i+2, j+1);
          if ( document.images[p].src.indexOf("yellow") >= 0  ) {
            return true;
          }
        }
      }
    }
  }
  return false;
}

function checkSolution() {

        if ( canSlide() ) {
          if ( !existPair() ) {
            alert("No space left to put more 2-by-1 tiles but some tile may slide!");
          }
          return;
        }

        if ( sr.charAt(1) != 'a' ||  sr.charAt(0) != 's' ) {
          return;
        }

        sum = 0;
        url = "http://sakharov.net/puzzle/gunportsolved.html?SOLUTION=";
        for (i = 0; i < document.images.length; i++) {
          if ( document.images[i].src.indexOf("yellow") >= 0 ) {
            sum++;
          }
        }
        for (i = 0; i < 8; i++) {
            for (j = 0; j < 8; j++) {
              k = findImage(i+1, j+1);
              if ( document.images[k].src.indexOf("yellow") >= 0 ) {
                url += 'y';
              } else if ( document.images[k].src.indexOf("blue") >= 0 ) {
                url += 'u';
              } else if ( document.images[k].src.indexOf("oranger") >= 0 ) {
                url += 'r';
              } else if ( document.images[k].src.indexOf("orangeb") >= 0 ) {
                url += 'b';
              } else if ( document.images[k].src.indexOf("orangel") >= 0 ) {
                url += 'l';
              } else if ( document.images[k].src.indexOf("oranget") >= 0 ) {
                url += 't';
              }
            }
        }
	if ( sum >= maxim[height][width] ) {
          location.replace(url);
	} else {
          alert("No tile can slide but the number of holes is not maximum. The maximum for this rectangle is " + maxim[height][width]);
        }
}

function swapSquare(m, n) {
      //alert("click " + m + n);
      sr = location.search;
      sr = sr.substring(sr.indexOf("SRC=")+4);
      if ( sr.length < 8 || sr.charAt(5) != 'r' ||  sr.charAt(7) != 'v' ) {
          return;
      }

      document.theForm.remove.checked = false;
      if ( removeMode ) {
        removeTile(m, n);
        removeMode = false;
        return;
      }

      if ( sr.charAt(4) != 'a' ||  sr.charAt(2) != 'k' ) {
          return;
      }

      i = findImage(m, n);
      //alert(document.images[i].src);
      if ( document.images[i].src.indexOf("yellow") >= 0 ) {

        if ( n+1 <= width ) {
          j = findImage(m, n+1);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/oranger.gif";
            document.images[j].src = "../image/orangel.gif";
            checkSolution();
            return;
          }
        }
        if ( m+1 <= height ) {
          j = findImage(m+1, n);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/orangeb.gif";
            document.images[j].src = "../image/oranget.gif";
            checkSolution();
            return;
          }
        }
        if ( n-1 >= 1 ) {
          j = findImage(m, n-1);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/orangel.gif";
            document.images[j].src = "../image/oranger.gif";
            checkSolution();
            return;
          }
        }
        if ( m-1 >= 1 ) {
          j = findImage(m-1, n);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/oranget.gif";
            document.images[j].src = "../image/orangeb.gif";
            checkSolution();
            return;
          }
        }
        alert("Impossible to place 2-by-1 tile here!");

      } else if ( document.images[i].src.indexOf("oranger") >= 0 ) {

        k = findImage(m, n+1);
        if ( m+1 <= height ) {
          j = findImage(m+1, n);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/orangeb.gif";
            document.images[j].src = "../image/oranget.gif";
            document.images[k].src = "../image/yellow.gif";
            checkSolution();
            return;
          }
        }
        if ( n-1 >= 1 ) {
          j = findImage(m, n-1);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/orangel.gif";
            document.images[j].src = "../image/oranger.gif";
            document.images[k].src = "../image/yellow.gif";
            checkSolution();
            return;
          }
        }
        if ( m-1 >= 1 ) {
          j = findImage(m-1, n);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/oranget.gif";
            document.images[j].src = "../image/orangeb.gif";
            document.images[k].src = "../image/yellow.gif";
            checkSolution();
            return;
          }
        }
        alert("Impossible to rotate 2-by-1 tile!");

      } else if ( document.images[i].src.indexOf("orangeb") >= 0 ) {

        k = findImage(m+1, n);
        if ( n-1 >= 1 ) {
          j = findImage(m, n-1);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/orangel.gif";
            document.images[j].src = "../image/oranger.gif";
            document.images[k].src = "../image/yellow.gif";
            checkSolution();
            return;
          }
        }
        if ( m-1 >= 1 ) {
          j = findImage(m-1, n);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/oranget.gif";
            document.images[j].src = "../image/orangeb.gif";
            document.images[k].src = "../image/yellow.gif";
            checkSolution();
            return;
          }
        }
        if ( n+1 <= width ) {
          j = findImage(m, n+1);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/oranger.gif";
            document.images[j].src = "../image/orangel.gif";
            document.images[k].src = "../image/yellow.gif";
            checkSolution();
            return;
          }
        }
        alert("Impossible to rotate 2-by-1 tile here!");

      } else if ( document.images[i].src.indexOf("orangel") >= 0 ) {

        k = findImage(m, n-1);
        if ( m-1 >= 1 ) {
          j = findImage(m-1, n);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/oranget.gif";
            document.images[j].src = "../image/orangeb.gif";
            document.images[k].src = "../image/yellow.gif";
            checkSolution();
            return;
          }
        }
        if ( n+1 <= width ) {
          j = findImage(m, n+1);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/oranger.gif";
            document.images[j].src = "../image/orangel.gif";
            document.images[k].src = "../image/yellow.gif";
            checkSolution();
            return;
          }
        }
        if ( m+1 <= height ) {
          j = findImage(m+1, n);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/orangeb.gif";
            document.images[j].src = "../image/oranget.gif";
            document.images[k].src = "../image/yellow.gif";
            checkSolution();
            return;
          }
        }
        alert("Impossible to rotate 2-by-1 tile here!");

      } else if ( document.images[i].src.indexOf("oranget") >= 0 ) {

        k = findImage(m-1, n);
        if ( n+1 <= width ) {
          j = findImage(m, n+1);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/oranger.gif";
            document.images[j].src = "../image/orangel.gif";
            document.images[k].src = "../image/yellow.gif";
            checkSolution();
            return;
          }
        }
        if ( m+1 <= height ) {
          j = findImage(m+1, n);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/orangeb.gif";
            document.images[j].src = "../image/oranget.gif";
            document.images[k].src = "../image/yellow.gif";
            checkSolution();
            return;
          }
        }
        if ( n-1 >= 1 ) {
          j = findImage(m, n-1);
          if ( document.images[j].src.indexOf("yellow") >= 0 ) {
            document.images[i].src = "../image/orangel.gif";
            document.images[j].src = "../image/oranger.gif";
            document.images[k].src = "../image/yellow.gif";
            checkSolution();
            return;
          }
        }
        alert("Impossible to rotate 2-by-1 tile here!");

      }

}

// --------------------

function load_solved_f() {

  var k1;
  var k2;

  last = 0;
  imgInd = new Array(8);
  for (i = 0; i < 9; i++) {
    imgInd[i] = new Array(9);
  }
  var r;
  for (r = 0; r < document.images.length; r++) {
      if ( document.images[r].name.substring(0, 7) == "problem" ) {
        var fr = document.images[r].name.charAt(7);
        var sc = document.images[r].name.charAt(8);
        imgInd[fr][sc] = r;
      }
  }
  var solution = location.search;
  var n = solution.indexOf("SOLUTION=");
  solution = solution.substring(n+9);
  //alert("A " + solution.charAt(0) + solution.charAt(1) + solution.charAt(2) + solution.charAt(3));

  for (i = 0; i <64; i++) {
    k1 = Math.floor(i/8);
    k2 = i - k1*8;
    k = findImage(k1+1, k2+1);
    //alert(k1 + " " + k2 + " " + k);
    if ( solution.charAt(i) == 'y' ) {
      document.images[k].src = "../image/yellow.gif";
    } else if ( solution.charAt(i) == 'u' ) {
      document.images[k].src = "../image/blue.gif";
    } else if ( solution.charAt(i) == 'r' ) {
      document.images[k].src = "../image/oranger.gif";
    } else if ( solution.charAt(i) == 'b' ) {
      document.images[k].src = "../image/orangeb.gif";
    } else if ( solution.charAt(i) == 'l' ) {
      document.images[k].src = "../image/orangel.gif";
    } else if ( solution.charAt(i) == 't' ) {
      document.images[k].src = "../image/oranget.gif";
    }
  }

}

