
/* Copyright (c) 2003 Timothy Sakharov, Alexander Sakharov */

var i;
var j;
var k;
var sr;
var cnt;

function countGrey() {
	cnt = 0;
        if ( sr.length < 5 || sr.charAt(1) != sr.charAt(4) ) {
          cnt += 4;
        }
	if ( document.problem12.src.indexOf("ghbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem14.src.indexOf("ghbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem16.src.indexOf("ghbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem21.src.indexOf("gvbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem23.src.indexOf("gvbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem25.src.indexOf("gvbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem27.src.indexOf("gvbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem32.src.indexOf("ghbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem34.src.indexOf("ghbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem36.src.indexOf("ghbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem41.src.indexOf("gvbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem43.src.indexOf("gvbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem45.src.indexOf("gvbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem47.src.indexOf("gvbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem52.src.indexOf("ghbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem54.src.indexOf("ghbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem56.src.indexOf("ghbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem61.src.indexOf("gvbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem63.src.indexOf("gvbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem65.src.indexOf("gvbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem72.src.indexOf("gvbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem74.src.indexOf("ghbar.gif") >= 0 ) {
		cnt++;
	}
        if ( document.problem76.src.indexOf("ghbar.gif") >= 0 ) {
		cnt++;
	}
	return cnt;
}

function swapColor(m, n) {
  //alert("AAA");
        sr = location.search;
        sr = sr.substring(sr.indexOf("SRC=")+4);

	k = countGrey();

// row 1
	if ( m == 1 && n == 2 && document.problem12.src.indexOf("bhbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem12.src = "../image/ghbar.gif";
		}
	} else if ( m == 1 && n == 2 && document.problem12.src.indexOf("ghbar.gif") >= 0 ) {
		document.problem12.src = "../image/bhbar.gif";
	} else if ( m == 1 && n == 4 && document.problem14.src.indexOf("bhbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem14.src = "../image/ghbar.gif";
		}
	} else if ( m == 1 && n == 4 && document.problem14.src.indexOf("ghbar.gif") >= 0 ) {
		document.problem14.src = "../image/bhbar.gif";
	} else if ( m == 1 && n == 6 && document.problem16.src.indexOf("bhbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem16.src = "../image/ghbar.gif";
		}
	} else if ( m == 1 && n == 6 && document.problem16.src.indexOf("ghbar.gif") >= 0 ) {
		document.problem16.src = "../image/bhbar.gif";

// row 2
	} else if ( m == 2 && n == 1 && document.problem21.src.indexOf("bvbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem21.src = "../image/gvbar.gif";
		}
	} else if ( m == 2 && n == 1 && document.problem21.src.indexOf("gvbar.gif") >= 0 ) {
		document.problem21.src = "../image/bvbar.gif";
		k--;
	} else if ( m == 2 && n == 3 && document.problem23.src.indexOf("bvbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem23.src = "../image/gvbar.gif";
		}
	} else if ( m == 2 && n == 3 && document.problem23.src .indexOf("gvbar.gif") >= 0 ) {
		document.problem23.src = "../image/bvbar.gif";
	} else if ( m == 2 && n == 5 && document.problem25.src.indexOf("bvbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem25.src = "../image/gvbar.gif";
		}
	} else if ( m == 2 && n == 5 && document.problem25.src.indexOf("gvbar.gif") >= 0 ) {
		document.problem25.src = "../image/bvbar.gif";
	} else if ( m == 2 && n == 7 && document.problem27.src.indexOf("bvbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem27.src = "../image/gvbar.gif";
		}
	} else if ( m == 2 && n == 7 && document.problem27.src.indexOf("gvbar.gif") >= 0 ) {
		document.problem27.src = "../image/bvbar.gif";

// row 3
	} else if ( m == 3 && n == 2 && document.problem32.src.indexOf("bhbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem32.src = "../image/ghbar.gif";
		}
	} else if ( m == 3 && n == 2 && document.problem32.src.indexOf("ghbar.gif") >= 0 ) {
		document.problem32.src = "../image/bhbar.gif";
		k--;
	} else if ( m == 3 && n == 4 && document.problem34.src.indexOf("bhbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem34.src = "../image/ghbar.gif";
		}
	} else if ( m == 3 && n == 4 && document.problem34.src.indexOf("ghbar.gif") >= 0 ) {
		document.problem34.src = "../image/bhbar.gif";
	} else if ( m == 3 && n == 6 && document.problem36.src.indexOf("bhbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem36.src = "../image/ghbar.gif";
		}
	} else if ( m == 3 && n == 6 && document.problem36.src.indexOf("ghbar.gif") >= 0 ) {
		document.problem36.src = "../image/bhbar.gif";

// row 4
	} else if ( m == 4 && n == 1 && document.problem41.src.indexOf("bvbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem41.src = "../image/gvbar.gif";
		}
	} else if ( m == 4 && n == 1 && document.problem41.src.indexOf("gvbar.gif") >= 0 ) {
		document.problem41.src = "../image/bvbar.gif";
		k--;
	} else if ( m == 4 && n == 3 && document.problem43.src.indexOf("bvbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem43.src = "../image/gvbar.gif";
		}
	} else if ( m == 4 && n == 3 && document.problem43.src.indexOf("gvbar.gif") >= 0 ) {
		document.problem43.src = "../image/bvbar.gif";
	} else if ( m == 4 && n == 5 && document.problem45.src.indexOf("bvbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem45.src = "../image/gvbar.gif";
		}
	} else if ( m == 4 && n == 5 && document.problem45.src.indexOf("gvbar.gif") >= 0 ) {
		document.problem45.src = "../image/bvbar.gif";
	} else if ( m == 4 && n == 7 && document.problem47.src.indexOf("bvbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem47.src = "../image/gvbar.gif";
		}
	} else if ( m == 4 && n == 7 && document.problem47.src.indexOf("gvbar.gif") >= 0 ) {
		document.problem47.src = "../image/bvbar.gif";

// row 5
	} else if ( m == 5 && n == 2 && document.problem52.src.indexOf("bhbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem52.src = "../image/ghbar.gif";
		}
	} else if ( m == 5 && n == 2 && document.problem52.src.indexOf("ghbar.gif") >= 0 ) {
		document.problem52.src = "../image/bhbar.gif";
		k--;
	} else if ( m == 5 && n == 4 && document.problem54.src.indexOf("bhbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem54.src = "../image/ghbar.gif";
		}
	} else if ( m == 5 && n == 4 && document.problem54.src.indexOf("ghbar.gif") >= 0 ) {
		document.problem54.src = "../image/bhbar.gif";
	} else if ( m == 5 && n == 6 && document.problem56.src.indexOf("bhbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem56.src = "../image/ghbar.gif";
		}
	} else if ( m == 5 && n == 6 && document.problem56.src.indexOf("ghbar.gif") >= 0 ) {
		document.problem56.src = "../image/bhbar.gif";

// row 6
	} else if ( m == 6 && n == 1 && document.problem61.src .indexOf("bvbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem61.src = "../image/gvbar.gif";
		}
	} else if ( m == 6 && n == 1 && document.problem61.src .indexOf("gvbar.gif") >= 0 ) {
		document.problem61.src = "../image/bvbar.gif";
		k--;
	} else if ( m == 6 && n == 3 && document.problem63.src.indexOf("bvbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem63.src = "../image/gvbar.gif";
		}
	} else if ( m == 6 && n == 3 && document.problem63.src.indexOf("gvbar.gif") >= 0 ) {
		document.problem63.src = "../image/bvbar.gif";
	} else if ( m == 6 && n == 5 && document.problem65.src.indexOf("bvbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem65.src = "../image/gvbar.gif";
		}
	} else if ( m == 6 && n == 5 && document.problem65.src.indexOf("gvbar.gif") >= 0 ) {
		document.problem65.src = "../image/bvbar.gif";
	} else if ( m == 6 && n == 7 && document.problem67.src.indexOf("bvbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem67.src = "../image/gvbar.gif";
		}
	} else if ( m == 6 && n == 7 && document.problem67.src.indexOf("gvbar.gif") >= 0 ) {
		document.problem67.src = "../image/bvbar.gif";

// row 7
	} else if ( m == 7 && n == 2 && document.problem72.src.indexOf("bhbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem72.src = "../image/ghbar.gif";
		}
	} else if ( m == 7 && n == 2 && document.problem72.src.indexOf("ghbar.gif") >= 0 ) {
		document.problem72.src = "../image/bhbar.gif";
		k--;
	} else if ( m == 7 && n == 4 && document.problem74.src.indexOf("bhbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem74.src = "../image/ghbar.gif";
		}
	} else if ( m == 7 && n == 4 && document.problem74.src.indexOf("ghbar.gif") >= 0 ) {
		document.problem74.src = "../image/bhbar.gif";
	} else if ( m == 7 && n == 6 && document.problem76.src.indexOf("bhbar.gif") >= 0 ) {
		if ( k >= 8 ) {
			alert("You removed too many sticks!");
			return;
		} else {
			document.problem76.src = "../image/ghbar.gif";
		}
	} else if ( m == 7 && n == 6 && document.problem76.src .indexOf("ghbar.gif") >= 0 ) {
		document.problem76.src = "../image/bhbar.gif";
	}

        if ( sr.length < 5 || sr.charAt(2) != 'k' ) {
          return;
        }
//alert("HHH" + k);
// check the state
	if ( k == 7
	&& document.problem23.src.indexOf("gvbar.gif") >= 0
	&& document.problem25.src.indexOf("gvbar.gif") >= 0
	&& document.problem32.src.indexOf("ghbar.gif") >= 0
	&& document.problem36.src.indexOf("ghbar.gif") >= 0
	&& document.problem52.src.indexOf("ghbar.gif") >= 0
	&& document.problem56.src.indexOf("ghbar.gif") >= 0
	&& document.problem63.src.indexOf("gvbar.gif") >= 0
	&& document.problem65.src.indexOf("gvbar.gif") >= 0 ) {
		location.replace("http://sakharov.net/puzzle/solved.html");
	}

}

