
/* Copyright (c) 2003 Timothy Sakharov, Alexander Sakharov */

  var itemText;
  var itemVal;
  var ind;

  var i;
  var tmp;
  var sr;

  var init;
  var step;
  var total;
  var pick;
  var weighting;
  var remain;
  var left;
  var right;
  var minimum;

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);
    init = total;
    step = 0;

    sr = location.search;
    sr = sr.substring(sr.indexOf("SRC="));

    if ( sr.length < 9 || sr.charAt(7) != 'h' || sr.charAt(11) != 'v' ) {
          total = -1;
          init = total;
    }
}

function pan_strt_f() {
    if ( total == -1 ) {
      alert("You have to select the total number of balls first!");
      return false;
    }

    ind = document.theForm_strt.pan.selectedIndex;
    itemText = document.theForm_strt.pan.options[ind].text;
    itemValue = document.theForm_strt.pan.options[ind].value;
    if ( itemValue.charAt(0) == '-' ) {
      alert("Incorrect selection");
      return false;
    }

    pick = parseInt(itemValue);
    if ( pick*2 > total ) {
      alert("Not enough balls");
      return false;
    }
    step++;
    location.replace("http://sakharov.net/puzzle/weight_itrm.html?SRC=sakharov&STAT=" + init + "Z" + step + "&TTL=" + total + "&PK=" + pick);
}


// -----

function load_f() {
      var t = location.search;
      var n = t.indexOf("TTL=");
      var m;

      total = parseInt(t.substring(n+4));
      n = t.indexOf("STAT=");
      var st = t.substring(n+5);
      n = st.indexOf("Z");
      m = st.indexOf("&");
      init = parseInt(st.substring(0, n));
      step = parseInt(st.substring(n+1, m));

      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 total_f() {
    alert("This selection cannot be changed until the end of the game!");
    return false;
}

function pan_f() {
    ind = document.theForm.pan.selectedIndex;
    itemText = document.theForm.pan.options[ind].text;
    itemValue = document.theForm.pan.options[ind].value;
    if ( itemValue.charAt(0) == '-' ) {
      alert("Incorrect selection");
      return false;
    }

    sr = location.search;
    sr = sr.substring(sr.indexOf("SRC="));

    if ( sr.length < 9 || sr.charAt(6) != 'k' || sr.charAt(9) != 'r' ) {
          return false;
    }

    pick = parseInt(itemValue);
    if ( pick*2 > total ) {
      alert("Not enough balls");
      return false;
    }
    step++;
    location.replace("http://sakharov.net/puzzle/weight_itrm.html?SRC=sakharov&STAT=" + init + "Z" + step + "&TTL=" + total + "&PK=" + pick);
}


// -----

function load_itrm_f() {
    var t = location.search;
    var n = t.indexOf("TTL=");
    var m = t.indexOf("PK=");
    total = parseInt(t.substring(n+4, m));
    pick = parseInt(t.substring(m+3));

    n = t.indexOf("STAT=");
    var st = t.substring(n+5);
    n = st.indexOf("Z");
    m = st.indexOf("&");
    init = parseInt(st.substring(0, n));
    step = parseInt(st.substring(n+1, m));

    //alert("T " + total + "P " + pick + " STEP " + st.substring(n+1) );

    for ( i = document.theForm_itrm.total.options.length - 1; i >= 0; i-- ) {
        document.theForm_itrm.total.options[i] = null;
    }
    document.theForm_itrm.total.options[0]
        = new Option(total, total, false, false);

    for ( i = document.theForm_itrm.pan.options.length - 1; i >= 0; i-- ) {
        document.theForm_itrm.pan.options[i] = null;
    }
    document.theForm_itrm.pan.options[0]
        = new Option(pick, pick, false, false);

    for ( i = document.theForm_itrm.heavier.options.length - 1; i >= 0; i-- ) {
        document.theForm_itrm.heavier.options[i] = null;
    }
    if ( total > pick*3 ) {
      weighting = "equal";
      document.theForm_itrm.heavier.options[0]
        = new Option("Equal Weight", "equal", false, false);
    } else if ( total < pick*3 ) {
      tmp = Math.floor(Math.random()*2);
      if ( tmp == 0 ) {
        weighting = "left";
        document.theForm_itrm.heavier.options[0]
        = new Option("Left Pan Heavier", "left", false, false);
      } else {
        weighting = "right";
        document.theForm_itrm.heavier.options[0]
        = new Option("Right Pan Heavier", "right", false, false);
      }
    } else {
      tmp = Math.floor(Math.random()*3);
      if ( tmp == 0 ) {
        weighting = "left";
        document.theForm_itrm.heavier.options[0]
        = new Option("Left Pan Heavier", "left", false, false);
      } else if ( tmp == 1 ) {
        weighting = "right";
        document.theForm_itrm.heavier.options[0]
        = new Option("Right Pan Heavier", "right", false, false);
      } else {
        weighting = "equal";
        document.theForm_itrm.heavier.options[0]
        = new Option("Equal Weight", "equal", false, false);
      }
    }
}

function total_itrm_f() {
  alert("This selection was made earlier and cannot be changed anymore!");
  return false;
}

function pan_itrm_f() {
  alert("This selection was made earlier and cannot be changed anymore!");
  return false;
}

function heavier_f() {
  alert("This selection cannot be changed - it is set by the computer based on the weights of the balls!");
  return false;
}

function which_f() {
    ind = document.theForm_itrm.which.selectedIndex;
    itemText = document.theForm_itrm.which.options[ind].text;
    itemValue = document.theForm_itrm.which.options[ind].value;
    if ( itemValue.charAt(0) == '-' ) {
      alert("Incorrect selection");
      return false;
    }

    sr = location.search;
    sr = sr.substring(sr.indexOf("SRC="));

    if ( sr.length < 9 || sr.charAt(7) != 'h' || sr.charAt(10) != 'o' ) {
          return false;
    }

    //alert(itemValue + "   " + weighting);
    if ( itemValue.indexOf("remain") >= 0 ) {
      if ( weighting.indexOf("equal") >= 0 ) {
        total = total - pick*2;
        if ( total > 1 )
        location.replace("http://sakharov.net/puzzle/weight.html?SRC=sakharov&STAT=" + init + "Z" + step + "&TTL=" + total);
      } else if ( weighting.indexOf("left") >= 0 ) {
        alert("Incorrect - the heavier ball is in another set");
        return false;
      } else if ( weighting.indexOf("right") >= 0 ) {
        alert("Incorrect - the heavier ball is in another set");
        return false;
      }
    } else if ( itemValue.indexOf("left") >= 0 ) {
      if ( weighting.indexOf("equal") >= 0 ) {
        alert("Incorrect - the heavier ball is in another set");
        return false;
      } else if ( weighting.indexOf("left") >= 0 ) {
        total = pick;
        if ( total > 1 )
        location.replace("http://sakharov.net/puzzle/weight.html?SRC=sakharov&STAT=" + init + "Z" + step + "&TTL=" + total);
      } else if ( weighting.indexOf("right") >= 0 ) {
        alert("Incorrect - the heavier ball is in another set");
        return false;
      }
    } else if ( itemValue.indexOf("right") >= 0 ) {
      if ( weighting.indexOf("equal") >= 0 ) {
        alert("Incorrect - the heavier ball is in another set");
        return false;
      } else if ( weighting.indexOf("left") >= 0 ) {
        alert("Incorrect - the heavier ball is in another set");
        return false;
      } else if ( weighting.indexOf("right") >= 0 ) {
        total = pick;
        if ( total > 1 )
        location.replace("http://sakharov.net/puzzle/weight.html?SRC=sakharov&STAT=" + init + "Z" + step + "&TTL=" + total);
      }
    }


    var lost = false;
    var minim;
    if ( init <= 9 && step > 2 ) { lost = true; minim = 2; }
    if ( init <= 27 && step > 3 ) { lost = true; minim = 3; }
    if ( step > 4 ) { lost = true; minim = 4; }

    if ( !lost && total == 1 ) {
      location.replace("http://sakharov.net/puzzle/weightsolved.html");
    } else if ( total == 1 ) {
      alert("You found the heavier ball but lost because you did not do it in the minimal number of steps. "
      + "The minimal number of weighting for " + init + " balls is " + minim + " and you did " + step);
      return false;
    }
}

function submit_start() {
  location.replace("http://sakharov.net/puzzle/weight_strt.html?SRC=sakharov");
}

function submit_itrm_start() {
  location.replace("http://sakharov.net/puzzle/weight_strt.html?SRC=sakharov");
}


