
/* Copyright (c) 2003 Timothy Sakharov, Alexander Sakharov */

var i;
var j;
var k;

var cnt1;
var cnt2;
var sr;

function mis2boat() {
  if ( sr.length < 5 || sr.charAt(6) != 'o' ) {
    return false;
  }
  if ( document.boat1.src.indexOf("spacec.gif") >= 0 ) {
    document.boat1.src = "../image/mis.gif";
    return true;
  } else if ( document.boat2.src.indexOf("spacec.gif") >= 0 ) {
    document.boat2.src = "../image/mis.gif";
    return true;
  }
  return false;
}

function can2boat() {
  if ( sr.length < 5 || sr.charAt(1) != sr.charAt(4) ) {
    return false;
  }
  if ( document.boat1.src.indexOf("spacec.gif") >= 0 ) {
    document.boat1.src = "../image/can.gif";
    return true;
  } else if ( document.boat2.src.indexOf("spacec.gif") >= 0 ) {
    document.boat2.src = "../image/can.gif";
    return true;
  }
  return false;
}

function swapLeftMis(n) {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.leftsp.width != 10 ) {
    alert("Boat is on the other side!");
    return;
  }
  if ( n == 1
  && document.leftmis1.src.indexOf("mis.gif") >= 0 ) {
    if ( mis2boat() ) {
      document.leftmis1.src = "../image/spacec.gif";
    }
  } else if ( n == 2
  && document.leftmis2.src.indexOf("mis.gif") >= 0 ) {
    if ( mis2boat() ) {
      document.leftmis2.src = "../image/spacec.gif";
    }
  } else if ( n == 3
  && document.leftmis3.src.indexOf("mis.gif") >= 0 ) {
    if ( mis2boat() ) {
      document.leftmis3.src = "../image/spacec.gif";
    }
  }
}

function swapLeftCan(n) {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.leftsp.width != 10 ) {
    alert("Boat is on the other side!");
    return;
  }
  if ( n == 1
  && document.leftcan1.src.indexOf("can.gif") >= 0 ) {
    if ( can2boat() ) {
      document.leftcan1.src = "../image/spacec.gif";
    }
  } else if ( n == 2
  && document.leftcan2.src.indexOf("can.gif") >= 0 ) {
    if ( can2boat() ) {
      document.leftcan2.src = "../image/spacec.gif";
    }
  } else if ( n == 3
  && document.leftcan3.src.indexOf("can.gif") >= 0 ) {
    if ( can2boat() ) {
      document.leftcan3.src = "../image/spacec.gif";
    }
  }
}

function swapRightMis(n) {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.rightsp.width != 10 ) {
    alert("Boat is on the other side!");
    return;
  }
  if ( n == 1
  && document.rightmis1.src.indexOf("mis.gif") >= 0 ) {
    if ( mis2boat() ) {
      document.rightmis1.src = "../image/spacec.gif";
    }
  } else if ( n == 2
  && document.rightmis2.src.indexOf("mis.gif") >= 0 ) {
    if ( mis2boat() ) {
      document.rightmis2.src = "../image/spacec.gif";
    }
  } else if ( n == 3
  && document.rightmis3.src.indexOf("mis.gif") >= 0 ) {
    if ( mis2boat() ) {
      document.rightmis3.src = "../image/spacec.gif";
    }
  }
}

function swapRightCan(n) {
  sr = location.search;
  sr = sr.substring(sr.indexOf("SRC=")+4);

  if ( document.rightsp.width != 10 ) {
    alert("Boat is on the other side!");
    return;
  }
  if ( n == 1
  && document.rightcan1.src.indexOf("can.gif") >= 0 ) {
    if ( can2boat() ) {
      document.rightcan1.src = "../image/spacec.gif";
    }
  } else if ( n == 2
  && document.rightcan2.src.indexOf("can.gif") >= 0 ) {
    if ( can2boat() ) {
      document.rightcan2.src = "../image/spacec.gif";
    }
  } else if ( n == 3
  && document.rightcan3.src.indexOf("can.gif") >= 0 ) {
    if ( can2boat() ) {
      document.rightcan3.src = "../image/spacec.gif";
    }
  }
}

function swapBoat(n) {
  if ( document.rightsp.width == 10 ) {
    if ( n == 1
    && document.boat1.src.indexOf("mis.gif") >= 0 ) {
      if ( document.rightmis1.src.indexOf("spacec.gif") >= 0 ) {
        document.rightmis1.src = "../image/mis.gif";
      } else if ( document.rightmis2.src.indexOf("spacec.gif") >= 0 ) {
        document.rightmis2.src = "../image/mis.gif";
      } else if ( document.rightmis3.src.indexOf("spacec.gif") >= 0 ) {
        document.rightmis3.src = "../image/mis.gif";
      }
      document.boat1.src = "../image/spacec.gif";
    } else if ( n == 1
    && document.boat1.src.indexOf("can.gif") >= 0 ) {
      if ( document.rightcan1.src.indexOf("spacec.gif") >= 0 ) {
        document.rightcan1.src = "../image/can.gif";
      } else if ( document.rightcan2.src.indexOf("spacec.gif") >= 0 ) {
        document.rightcan2.src = "../image/can.gif";
      } else if ( document.rightcan3.src.indexOf("spacec.gif") >= 0 ) {
        document.rightcan3.src = "../image/can.gif";
      }
      document.boat1.src = "../image/spacec.gif";
    } else if ( n == 2
    && document.boat2.src.indexOf("mis.gif") >= 0 ) {
      if ( document.rightmis1.src.indexOf("spacec.gif") >= 0 ) {
        document.rightmis1.src = "../image/mis.gif";
      } else if ( document.rightmis2.src.indexOf("spacec.gif") >= 0 ) {
        document.rightmis2.src = "../image/mis.gif";
      } else if ( document.rightmis3.src.indexOf("spacec.gif") >= 0 ) {
        document.rightmis3.src = "../image/mis.gif";
      }
      document.boat2.src = "../image/spacec.gif";
    } else if ( n == 2
    && document.boat2.src.indexOf("can.gif") >= 0 ) {
      if ( document.rightcan1.src.indexOf("spacec.gif") >= 0 ) {
        document.rightcan1.src = "../image/can.gif";
      } else if ( document.rightcan2.src.indexOf("spacec.gif") >= 0 ) {
        document.rightcan2.src = "../image/can.gif";
      } else if ( document.rightcan3.src.indexOf("spacec.gif") >= 0 ) {
        document.rightcan3.src = "../image/can.gif";
      }
      document.boat2.src = "../image/spacec.gif";
    }
  } else { // to the left
    if ( n == 1
    && document.boat1.src.indexOf("mis.gif") >= 0 ) {
      if ( document.leftmis1.src.indexOf("spacec.gif") >= 0 ) {
        document.leftmis1.src = "../image/mis.gif";
      } else if ( document.leftmis2.src.indexOf("spacec.gif") >= 0 ) {
        document.leftmis2.src = "../image/mis.gif";
      } else if ( document.leftmis3.src.indexOf("spacec.gif") >= 0 ) {
        document.leftmis3.src = "../image/mis.gif";
      }
      document.boat1.src = "../image/spacec.gif";
    } else if ( n == 1
    && document.boat1.src.indexOf("can.gif") >= 0 ) {
      if ( document.leftcan1.src.indexOf("spacec.gif") >= 0 ) {
        document.leftcan1.src = "../image/can.gif";
      } else if ( document.leftcan2.src.indexOf("spacec.gif") >= 0 ) {
        document.leftcan2.src = "../image/can.gif";
      } else if ( document.leftcan3.src.indexOf("spacec.gif") >= 0 ) {
        document.leftcan3.src = "../image/can.gif";
      }
      document.boat1.src = "../image/spacec.gif";
    } else if ( n == 2
    && document.boat2.src.indexOf("mis.gif") >= 0 ) {
      if ( document.leftmis1.src.indexOf("spacec.gif") >= 0 ) {
        document.leftmis1.src = "../image/mis.gif";
      } else if ( document.leftmis2.src.indexOf("spacec.gif") >= 0 ) {
        document.leftmis2.src = "../image/mis.gif";
      } else if ( document.leftmis3.src.indexOf("spacec.gif") >= 0 ) {
        document.leftmis3.src = "../image/mis.gif";
      }
      document.boat2.src = "../image/spacec.gif";
    } else if ( n == 2
    && document.boat2.src.indexOf("can.gif") >= 0 ) {
      if ( document.leftcan1.src.indexOf("spacec.gif") >= 0 ) {
        document.leftcan1.src = "../image/can.gif";
      } else if ( document.leftcan2.src.indexOf("spacec.gif") >= 0 ) {
        document.leftcan2.src = "../image/can.gif";
      } else if ( document.leftcan3.src.indexOf("spacec.gif") >= 0 ) {
        document.leftcan3.src = "../image/can.gif";
      }
      document.boat2.src = "../image/spacec.gif";
    }
  }
}

function checkNumber() {
  cnt1 = 0;
  cnt2 = 0;
  if ( document.leftmis1.src.indexOf("mis.gif") >= 0 ) {
        cnt1++;
  }
  if ( document.leftmis2.src.indexOf("mis.gif") >= 0 ) {
        cnt1++;
  }
  if ( document.leftmis3.src.indexOf("mis.gif") >= 0 ) {
        cnt1++;
  }
  if ( document.leftcan1.src.indexOf("can.gif") >= 0 ) {
        cnt2++;
  }
  if ( document.leftcan2.src.indexOf("can.gif") >= 0 ) {
        cnt2++;
  }
  if ( document.leftcan3.src.indexOf("can.gif") >= 0 ) {
        cnt2++;
  }

  if ( cnt2 > cnt1 ) {
    if ( document.leftmis1.src.indexOf("mis.gif") >= 0 ) {
        document.leftmis1.src = "../image/bone.gif";
    }
    if ( document.leftmis2.src.indexOf("mis.gif") >= 0 ) {
        document.leftmis2.src = "../image/bone.gif";
    }
    if ( document.leftmis3.src.indexOf("mis.gif") >= 0 ) {
        document.leftmis3.src = "../image/bone.gif";
    }
  }

  cnt1 = 0;
  cnt2 = 0;
  if ( document.rightmis1.src.indexOf("mis.gif") >= 0 ) {
        cnt1++;
  }
  if ( document.rightmis2.src.indexOf("mis.gif") >= 0 ) {
        cnt1++;
  }
  if ( document.rightmis3.src.indexOf("mis.gif") >= 0 ) {
        cnt1++;
  }
  if ( document.rightcan1.src.indexOf("can.gif") >= 0 ) {
        cnt2++;
  }
  if ( document.rightcan2.src.indexOf("can.gif") >= 0 ) {
        cnt2++;
  }
  if ( document.rightcan3.src.indexOf("can.gif") >= 0 ) {
        cnt2++;
  }

  if ( cnt2 > cnt1 ) {
    if ( document.rightmis1.src.indexOf("mis.gif") >= 0 ) {
        document.rightmis1.src = "../image/bone.gif";
    }
    if ( document.rightmis2.src.indexOf("mis.gif") >= 0 ) {
        document.rightmis2.src = "../image/bone.gif";
    }
    if ( document.rightmis3.src.indexOf("mis.gif") >= 0 ) {
        document.rightmis3.src = "../image/bone.gif";
    }
  }
}

function checkResult() {
  if ( document.rightmis1.src.indexOf("mis.gif") >= 0
  && document.rightmis2.src.indexOf("mis.gif") >= 0
  && document.rightmis3.src.indexOf("mis.gif") >= 0
  && document.rightcan1.src.indexOf("can.gif") >= 0
  && document.rightcan2.src.indexOf("can.gif") >= 0
  && document.rightcan3.src.indexOf("can.gif") >= 0 ) {
    location.replace("http://sakharov.net/puzzle/canmissolved.html");
  }
}

function submit_left() {
  if ( document.leftsp.width != 100 )
    return;
  if ( document.boat1.src.indexOf("spacec.gif") >= 0
  && document.boat2.src.indexOf("spacec.gif") >= 0 ) {
    alert("Nobody is in the boat!");
    return;
  }
  document.leftsp.width = 10;
  document.rightsp.width = 100;
  swapBoat(1);
  swapBoat(2);
  checkNumber();
  checkResult();
}

function submit_right() {
  if ( document.rightsp.width != 100 )
    return;
  if ( document.boat1.src.indexOf("spacec.gif") >= 0
  && document.boat2.src.indexOf("spacec.gif") >= 0 ) {
    alert("Nobody is in the boat!");
    return;
  }
  document.rightsp.width = 10;
  document.leftsp.width = 100;
  swapBoat(1);
  swapBoat(2);
  checkNumber();
  checkResult();
}

function submit_start() {
    document.leftsp.width = 10;
    document.rightsp.width = 100;
    document.leftmis1.src = "../image/mis.gif";
    document.leftmis2.src = "../image/mis.gif";
    document.leftmis3.src = "../image/mis.gif";
    document.leftcan1.src = "../image/can.gif";
    document.leftcan2.src = "../image/can.gif";
    document.leftcan3.src = "../image/can.gif";
    document.boat1.src = "../image/spacec.gif";
    document.boat2.src = "../image/spacec.gif";
    document.rightmis1.src = "../image/spacec.gif";
    document.rightmis2.src = "../image/spacec.gif";
    document.rightmis3.src = "../image/spacec.gif";
    document.rightcan1.src = "../image/spacec.gif";
    document.rightcan2.src = "../image/spacec.gif";
    document.rightcan3.src = "../image/spacec.gif";

}




