
/* Copyright (c) 2003 Timothy Sakharov, Alexander Sakharov */

  var itemText;
  var itemVal;
  var ind;

  var i;
  var tmp;
  var sr;

  var total;

function load_strt_f() {
    total = -1;
}

function total_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;
    }
    total = parseInt(itemValue);

    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/bywall.html?SRC=sakharov&TTL=" + total);
}


// -----

function load_f() {
      var t = location.search;
      var n = t.indexOf("TTL=");

      total = parseInt(t.substring(n+4));

      sr = t;
      sr = sr.substring(sr.indexOf("SRC="));
      if ( sr.length < 9 || sr.charAt(6) != 'k' || sr.charAt(11) != 'v' ) {
          return false;
      }

      for ( i = document.theForm.total.options.length - 1; i >= 0; i-- ) {
        document.theForm.total.options[i] = null;
      }
      document.theForm.total.options[0]
        = new Option(total, total, false, false);

}

function submit_f() {

    sr = location.search;
    sr = sr.substring(sr.indexOf("SRC="));
    if ( sr.length < 9 || sr.charAt(7) != 'h' || sr.charAt(10) != 'o' ) {
          return false;
    }

    var topleft = parseInt(document.theForm.topleft.value);
    if ( isNaN(topleft) ) topleft = 0;
    var top = parseInt(document.theForm.top.value);
    if ( isNaN(top) ) top = 0;
    var topright = parseInt(document.theForm.topright.value);
    if ( isNaN(topright) ) topright = 0;
    var left = parseInt(document.theForm.left.value);
    if ( isNaN(left) ) left = 0;
    var right = parseInt(document.theForm.right.value);
    if ( isNaN(right) ) right = 0;
    var bottomleft = parseInt(document.theForm.bottomleft.value);
    if ( isNaN(bottomleft) ) bottomleft = 0;
    var bottom = parseInt(document.theForm.bottom.value);
    if ( isNaN(bottom) ) bottom = 0;
    var bottomright = parseInt(document.theForm.bottomright.value);
    if ( isNaN(bottomright) ) bottomright = 0;

    var cnt = 0;
    var leftcnt = 0;
    var topcnt = 0;
    var rightcnt = 0;
    var bottomcnt = 0;

    leftcnt = topleft + left + bottomleft;
    topcnt = topleft + top + topright;
    rightcnt = topright + right + bottomright;
    bottomcnt = bottomleft + bottom + bottomright;

    //alert ( topleft + " " + top + " " + topright + " " + left + " " + right + " " + bottomleft + " " + bottom + " " + bottomright)
    if ( leftcnt != topcnt || topcnt != rightcnt || rightcnt != bottomcnt ) {
      alert("Incorrect - not equal number of boxes by walls");
      return false;
    } else if ( total != topleft + top + topright + left + right + bottomleft + bottom + bottomright ) {
      alert("Incorrect - the numbers do not sum up to total");
      return false;
    } else {
      location.replace("http://sakharov.net/puzzle/bywallsolved.html?TL=" + (topleft==0?"":topleft) + "&TT=" + (top==0?"":top) + "&TR=" + (topright==0?"":topright)
      + "&LL=" + (left==0?"":left) + "&RR=" + (right==0?"":right) + "&BL=" + (bottomleft==0?"":bottomleft) + "&BB=" + (bottom==0?"":bottom) + "&BR=" + (bottomright==0?"":bottomright) );
      return true;
    }

}

function total_f() {
    alert("This selection was made earlier and cannot be changed anymore!");
    return false;
}

function load_solved() {

    sr = location.search;

    tmp = sr.substring(sr.indexOf("TL="));
    document.theForm_solved.topleft.value = tmp.substring(3, tmp.indexOf("&"));

    tmp = sr.substring(sr.indexOf("TT="));
    document.theForm_solved.top.value = tmp.substring(3, tmp.indexOf("&"));

    tmp = sr.substring(sr.indexOf("TR="));
    document.theForm_solved.topright.value = tmp.substring(3, tmp.indexOf("&"));

    tmp = sr.substring(sr.indexOf("LL="));
    document.theForm_solved.left.value = tmp.substring(3, tmp.indexOf("&"));

    tmp = sr.substring(sr.indexOf("RR="));
    document.theForm_solved.right.value = tmp.substring(3, tmp.indexOf("&"));

    tmp = sr.substring(sr.indexOf("BL="));
    document.theForm_solved.bottomleft.value = tmp.substring(3, tmp.indexOf("&"));

    tmp = sr.substring(sr.indexOf("BB="));
    document.theForm_solved.bottom.value = tmp.substring(3, tmp.indexOf("&"));

    tmp = sr.substring(sr.indexOf("BR="));
    document.theForm_solved.bottomright.value = tmp.substring(3);

}


